Codeforces Round #630 (Div. 2)

B. Composite Coloring

A positive integer is called composite if it can be represented as a product of two positive integers, both greater than 1. For example, the following numbers are composite: 6, 4, 120, 27. The following numbers aren’t: 1, 2, 3, 17, 97.
Alice is given a sequence of n composite numbers a1,a2,…,an.
She wants to choose an integer m≤11 and color each element one of m colors from 1 to m so that:
for each color from 1 to m there is at least one element of this color;
each element is colored and colored exactly one color;
the greatest common divisor of any two elements that are colored the same color is greater than 1, i.e. gcd(ai,aj)>1 for each pair i,j if these elements are colored the same color.
Note that equal elements can be colored different colors — you just have to choose one of m colors for each of the indices from 1 to n.
Alice showed already that if all ai≤1000 then she can always solve the task by choosing some m≤11.
Help Alice to find the required coloring. Note that you don’t have to minimize or maximize the number of colors, you just have to find the solution with some m from 1 to 11.
Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases. Then the descriptions of the test cases follow.
The first line of the test case contains a single integer n (1≤n≤1000) — the amount of numbers in a sequence a.
The second line of the test case contains n composite integers a1,a2,…,an (4≤ai≤1000).
It is guaranteed that the sum of n over all test cases doesn’t exceed 104.
Output
For each test case print 2 lines. The first line should contain a single integer m (1≤m≤11) — the number of used colors. Consider colors to be numbered from 1 to m. The second line should contain any coloring that satisfies the above conditions. Print n integers c1,c2,…,cn (1≤ci≤m), where ci is the color of the i-th element. If there are multiple solutions then you can print any of them. Note that you don’t have to minimize or maximize the number of colors, you just have to find the solution with some m from 1 to 11.
Remember that each color from 1 to m should be used at least once. Any two elements of the same color should not be coprime (i.e. their GCD should be greater than 1).

题意
给定n个composite数(composite数就是合数,能分解成两个数的乘积),然后让你选m个数字,每个数字(1~m)各代表一种不同的颜色,给输入的n个数每个数选一个颜色。
要求:相同的数字可以选同一种颜色;两个不同的数字x,y,如果gcd(x,y)>1则x和y也可以选同一种颜色。还有就是选的m中颜色每种颜色都必须要用到。
思路
唯一分解定理:一个数字能够分解成若干个素数的乘积。又因为平方小于1000的质因数只有11个,所以最小质因数相同的染一个颜色即可。
code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int p[15]={2,3,5,7,11,13,17,19,23,29,31};
int ans[1010],a[1010];
int flag[15];
int main()
{
	int t,n,num;cin>>t;
	while(t--)
	{
		cin>>n;
		memset(flag,0,sizeof(flag));
		num=0;
		for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
		for(int i=1;i<=n;i++)
		{
			for(int j=0;j<11;j++)
			{
				if(a[i]%p[j]==0)
				{
					if(flag[j])ans[i]=flag[j];
					else flag[j]=++num,ans[i]=flag[j];
					break;
				}
			}
		}
		cout<<num<<endl;
		for(int i=1;i<=n;i++)
		printf("%d%c",ans[i],i==n?'\n':' '); 
	}
	return 0;
} 

C. K-Complete Word

outputstandard output
Word s of length n is called k-complete if
s is a palindrome, i.e. si=sn+1−i for all 1≤i≤n;
s has a period of k, i.e. si=sk+i for all 1≤i≤n−k.
For example, “abaaba” is a 3-complete word, while “abccba” is not.
Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word.
To do this Bob can choose some i (1≤i≤n) and replace the letter at position i with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word.
Note that Bob can do zero changes if the word s is already k-complete.
You are required to answer t test cases independently.
Input
The first line contains a single integer t (1≤t≤105) — the number of test cases.
The first line of each test case contains two integers n and k (1≤k<n≤2⋅105, n is divisible by k).
The second line of each test case contains a word s of length n.
It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2⋅105.
Output
For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word.
题意
给定一个长度为n的字符串,需要你修改其中几个字符使这个字符串变为周期为k的回文字符串,求修改字符串的最小个数使几个?
周期为k的回文字符串:比如一个字符串s,则有 s[i]=s[i+k]=s[i+2k]=…=s[n-i+1]=s[n-i+1-k]=s[n-i+1-2k]=…

思路
把s[i]、s[i+k]、s[i+2k]…、s[n-i+1]、s[n-i+1-k]、s[n-i+1-2k]这些字符全部修改为出现次数最多的字符,这样就达到了修改次数最小的要求。

code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char s[200010];
int flag[30]; //统计字母出现的次数 
bool w[200010];标记数组,用来判断某个字母是否被遍历过
int ans;
int main()
{
	int n,k,t;cin>>t;
	while(t--)
	{
		scanf("%d%d%s",&n,&k,s+1);
		ans=0;
		//memset(w,false,sizeof(w));
		for(int i=1;i<=n;i++) w[i] = false;
		for(int i=1;i<=n;i++)
		{
			if(w[i])continue;
			int num=0;//统计与i相关的字符个数 
			int maxx=0;//统计出现最多的字符 
			memset(flag,0,sizeof(flag));
			for(int j=i;j<=n;j+=k)
			{
				if(w[j])break;
				w[j]=true;
				num++;
				flag[s[j]-'a']++;
				maxx=max(maxx,flag[s[j]-'a']); 
			} 
			for(int j=n-i+1;j>=1;j-=k)
			{
				if(w[j])break;
				w[j]=true;
				num++;
				flag[s[j]-'a']++;
				maxx=max(maxx,flag[s[j]-'a']); 
			}
			ans=ans+num-maxx; 
		}
		cout<<ans<<endl;
	}
}

D. Walk on Matrix

Bob is playing a game named “Walk on Matrix”.
In this game, player is given an n×m matrix A=(ai,j), i.e. the element in the i-th row in the j-th column is ai,j. Initially, player is located at position (1,1) with score a1,1.
To reach the goal, position (n,m), player can move right or down, i.e. move from (x,y) to (x,y+1) or (x+1,y), as long as player is still on the matrix.
However, each move changes player’s score to the bitwise AND of the current score and the value at the position he moves to.
Bob can’t wait to find out the maximum score he can get using the tool he recently learnt — dynamic programming. Here is his algorithm for this problem.
在这里插入图片描述
However, each move changes player’s score to the bitwise AND of the current score and the value at the position he moves to.
Bob can’t wait to find out the maximum score he can get using the tool he recently learnt — dynamic programming. Here is his algorithm for this problem.
However, he suddenly realize that the algorithm above fails to output the maximum score for some matrix A. Thus, for any given non-negative integer k, he wants to find out an n×m matrix A=(ai,j) such that
1≤n,m≤500 (as Bob hates large matrix);
0≤ai,j≤3⋅105 for all 1≤i≤n,1≤j≤m (as Bob hates large numbers);
the difference between the maximum score he can get and the output of his algorithm is exactly k.
It can be shown that for any given integer k such that 0≤k≤105, there exists a matrix satisfying the above constraints.
Please help him with it!
Input
The only line of the input contains one single integer k (0≤k≤105).
Output
Output two integers n, m (1≤n,m≤500) in the first line, representing the size of the matrix.
Then output n lines with m integers in each line, ai,j in the (i+1)-th row, j-th column.
题意
假设现在有一个n⋅m的矩阵,每个位置的权值为ai,j。现在从(1,1)出发要到达(n,n),每次只能往下走或者往右走,代价为:当前价值x与ai,j的&,即x&ai,j。然后题目给出了一个dp算法dp[i ][ j ]=max(dp[ i-1][j]&a[i][j],dp[i][j-1]&a[i][j]),但是这个算法是不怎么正确滴。
要你构造出一个矩阵,这个矩阵有==》给定一个k,k给实际的值与dp算法结果的差值
思路
构造矩阵

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值