Codeforces Round #630 (Div. 2)

A. Exercising Walk

time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Alice has a cute cat. To keep her cat fit, Alice wants to design an exercising walk for her cat!

Initially, Alice’s cat is located in a cell (x,y) of an infinite grid. According to Alice’s theory, cat needs to move:

exactly a steps left: from (u,v) to (u−1,v);
exactly b steps right: from (u,v) to (u+1,v);
exactly c steps down: from (u,v) to (u,v−1);
exactly d steps up: from (u,v) to (u,v+1).
Note that the moves can be performed in an arbitrary order. For example, if the cat has to move 1 step left, 3 steps right and 2 steps down, then the walk right, down, left, right, right, down is valid.

Alice, however, is worrying that her cat might get lost if it moves far away from her. So she hopes that her cat is always in the area [x1,x2]×[y1,y2], i.e. for every cat’s position (u,v) of a walk x1≤u≤x2 and y1≤v≤y2 holds.

Also, note that the cat can visit the same cell multiple times.

Can you help Alice find out if there exists a walk satisfying her wishes?

Formally, the walk should contain exactly a+b+c+d unit moves (a to the left, b to the right, c to the down, d to the up). Alice can do the moves in any order. Her current position (u,v) should always satisfy the constraints: x1≤u≤x2, y1≤v≤y2. The staring point is (x,y).

You are required to answer t test cases independently.

Input
The first line contains a single integer t (1≤t≤103) — the number of testcases.

The first line of each test case contains four integers a, b, c, d (0≤a,b,c,d≤108, a+b+c+d≥1).

The second line of the test case contains six integers x, y, x1, y1, x2, y2 (−108≤x1≤x≤x2≤108, −108≤y1≤y≤y2≤108).

Output
For each test case, output “YES” in a separate line, if there exists a walk satisfying her wishes. Otherwise, output “NO” in a separate line.

You can print each letter in any case (upper or lower).

Example
inputCopy
6
3 2 2 2
0 0 -2 -2 2 2
3 1 4 1
0 0 -1 -1 1 1
1 1 1 1
1 1 1 1 1 1
0 0 0 1
0 0 0 0 0 1
5 1 1 1
0 0 -100 -100 0 100
1 1 5 1
0 0 -100 -100 100 0
outputCopy
Yes
No
No
Yes
Yes
Yes
Note
In the first test case, one valid exercising walk is
(0,0)→(−1,0)→(−2,0)→(−2,1)→(−2,2)→(−1,2)→(0,2)→(0,1)→(0,0)→(−1,0)

【题目大意:】
给出一个二维坐标中的范围,以及起点,能否找到一条路径是a次向左,b次向右,c次向下,d次向上 向左向右向上向下的顺序无所谓

//
// Created by DELL on 2020/3/31.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

int main(int argc,char* argv[]) {
    int T; int a,b,c,d,x,y,x1,x2,y1,y2;
    scanf("%d",&T);
    bool Judge = 1;
    while(T--) {
        scanf("%d %d %d %d",&a,&b,&c,&d);
        scanf("%d %d %d %d %d %d",&x,&y,&x1,&y1,&x2,&y2);
        Judge = 1;
        a -= b; c -= d;
        if(a >= 0 && x - x1 < a) Judge = 0;
        if(a < 0 && x2 - x < abs(a)) Judge = 0;
        if(c >=0 && y - y1 < c) Judge = 0;//这一句是粘贴上一句a忘记改成c   浪费了好长时间
        if(c < 0 && y2 - y < abs(c)) Judge = 0;
        if(x1 == x2 && (a || b)) Judge = 0;
        if(y1 == y2 && (c || d)) Judge = 0;
        if(Judge) printf("Yes\n");
        else printf("No\n");
    }

    return 0;
}

B. Composite Coloring

time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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).

Example
inputCopy
3
3
6 10 15
2
4 9
23
437 519 865 808 909 391 194 291 237 395 323 365 511 497 781 737 871 559 731 697 779 841 961
outputCopy
1
1 1 1
2
2 1
11
4 7 8 10 7 3 10 7 7 8 3 1 1 5 5 9 2 2 3 3 4 11 6
Note
In the first test case, gcd(6,10)=2, gcd(6,15)=3 and gcd(10,15)=5. Therefore, it’s valid to color all elements the same color. Note that there are other colorings which satisfy Alice’s requirement in this test case.

In the second test case there is only one element of each color, so the coloring definitely satisfies Alice’s requirement.

【思路】
按照每个数最小的质因数进行分类,每个最小的质因数给予一个编号(所有的数用过的质因数,没有用过的不算)

//
// Created by DELL on 2020/3/31.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#define Maxn 1002
using namespace std;
int tot = 0,prime[Maxn],a[Maxn],ans[Maxn],vis[Maxn];
inline bool is_prime(int x) {
    for(int i=2; i*i<=x; i++) if(x % i == 0 ) return false;
    return true;
}
int main(int argc,char* argv[]) {
    for(int i=2; i<=1000; i++) if(is_prime(i)) prime[++tot] = i;
    int T,n;   scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        int m = 0;
        memset(vis,0,sizeof(vis));
        for(int i=1; i<=n; i++){
            scanf("%d",&a[i]);
            for(int j=1; j<=tot; j++) if(a[i] % prime[j] == 0) {
                if(!vis[j]) vis[j] = ++m;
                ans[i] = vis[j]; break;
            }
        }
        printf("%d\n",m);
        for(int i=1; i<=n; i++) printf("%d ",ans[i]);
        printf("\n");
    }
    return 0;
}

C. K-Complete Word

time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
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.

Example
inputCopy
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
outputCopy
2
0
23
16
Note
In the first test case, one optimal solution is aaaaaa.

In the second test case, the given word itself is k-complete.

思路:我们先看需要满足的第二个条件,设dis=n/kdis=n/kdis=n/k:
s[1]=s[1+k]=s[1+2∗k]=……=s[1+(dis−1)∗k]s[1]=s[1+k]=s[1+2k]=……=s[1+(dis-1)k]
s[1]=s[1+k]=s[1+2∗k]=……=s[1+(dis−1)∗k]
s[2]=s[2+k]=s[2+2∗k]=……=s[2+(dis−1)∗k]s[2]=s[2+k]=s[2+2
k]=……=s[2+(dis-1)k]
s[2]=s[2+k]=s[2+2∗k]=……=s[2+(dis−1)∗k]
…………
……
s[k]=s[2∗k]=s[3∗k]=……=s[dis∗k]s[k]=s[2
k]=s[3
k]=……=s[dis*k]
s[k]=s[2∗k]=s[3∗k]=……=s[dis∗k]
此时我们再考虑第一个条件,即s要是一个回文串:
s[1]=s[n],s[2]=s[n−1],……s[1]=s[n],s[2]=s[n-1],……
s[1]=s[n],s[2]=s[n−1],……
结合上式化简,可以发现我们需要满足:
s[1]=s[k],s[2]=s[k−1],……s[1]=s[k],s[2]=s[k-1],……
s[1]=s[k],s[2]=s[k−1],……

也就是说s分成了dis个完全相同的部分,每一部分也是一个回文串
所以有:
s[1] = s[k] = s[k+1] = s[k + k] = ········s[n-k] = s[n]
s[2] = s[k-1] = s[k+2] = s[k+k-1] = ········s[n-k+1]=s[n-1]

所以有了解决办法 对于一个长度为k的部分,并且是其中的第i位,暴力扫描k-i位,并且检查其后面所有部分的i位和k-i位,取出现次数最多的字符
Ans += 2 * n / k - Max
特别注意: 当i是k的中间位置(k位奇数)时 Ans += n / k - Max

//
// Created by DELL on 2020/4/1.
//
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <string>
#define Maxn 200005
using namespace std;
char s[Maxn];
int main(int argc,char* argv[]) {
    int T,k,n;  scanf("%d",&T);
    int cnt[30];
    while(T--) {
        cin >> n >> k;
        scanf("%s",s);
        int Ans = 0;
        for(int i=0; i<(k&1?k/2+1:k/2); i++) {
            memset(cnt,0,sizeof(cnt));
            for(int j=0; i + j * k < n; j++) {
                cnt[s[i + j * k] - 'a']++;
                if(i + j * k != k - i - 1 + j * k) cnt[s[k - i - 1 + j * k] - 'a'] ++;
            }
            sort(cnt,cnt + 30);
            if((k & 1) && i == k / 2) Ans += n / k - cnt[29];
            else Ans += 2 * n / k - cnt[29];
        }
        printf("%d\n",Ans);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值