[Educational Codeforces Round 84]1327


1327A - Sum of Odd Integers[思维]
1327B - Princesses and Princes[思维]
1327C - Game with Chips[思维]
1327D - Infinite Path[ ]
1327E - Count The Blocks[找规律]
1327F - AND Segments[ ]




目录

1327A - Sum of Odd Integers[思维]

time limit per testmemory limit per testinputoutput
2 seconds256 megabytesstandard inputstandard output

Description:

You are given two integers n n n and k k k. Your task is to find if n n n can be represented as a sum of k k k distinct positive odd (not divisible by 2 2 2) integers or not.
You have to answer t t t independent test cases.

Input

The first line of the input contains one integer t ( 1 ≤ t ≤ 1 0 5 ) t (1≤t≤10^5) t(1t105) — the number of test cases.
The next t t t lines describe test cases. The only line of the test case contains two integers n n n and k ( 1 ≤ n , k ≤ 107 ) k (1≤n,k≤107) k(1n,k107).

Output

For each test case, print the answer — “ Y E S YES YES” (without quotes) if n n n can be represented as a sum of k k k distinct positive odd (not divisible by 2 2 2) integers and “ N O NO NO” otherwise.

Example input

6
3 1
4 2
10 3
10 2
16 4
16 5

Example output

YES
YES
NO
YES
YES
NO

Note

In the first test case, you can represent 3 3 3 as 3 3 3.
In the second test case, the only way to represent 4 4 4 is 1 + 3 1+3 1+3.
In the third test case, you cannot represent 10 10 10 as the sum of three distinct positive odd integers.
In the fourth test case, you can represent 10 10 10 as 3 + 7 3+7 3+7, for example.
In the fifth test case, you can represent 16 16 16 as 1 + 3 + 5 + 7 1+3+5+7 1+3+5+7.
In the sixth test case, you cannot represent 16 16 16 as the sum of five distinct positive odd integers.

分析:
题意:
是否存在 k k k个不同的奇数和为 n n n
做法:
有解的情况如下:
( n − k ) % 2 = = 0 (n - k) \% 2 == 0 (nk)%2==0,即 n , k n,k n,k的奇偶性相同
可以先考虑当 n % 2 = = 0 n \% 2 == 0 n%2==0时, k k k应该满足 k % 2 = = 0 k \% 2 == 0 k%2==0,偶=奇*偶
n % 2 ! = 0 n \% 2 != 0 n%2!=0 k k k应该满足 k % 2 ! = 0 k \% 2 != 0 k%2!=0,奇=奇*奇
k 2 ≤ n k^2 \leq n k2n
最小的 k k k个不同的奇数的和等于 k 2 k^2 k2
1 + 3 + . . . + ( 2 ∗ k − 1 ) = ( 1 + 2 ∗ k − 1 ) ∗ k / 2 = k 2 1 + 3 + ... + (2*k-1) = (1 + 2*k-1) * k / 2 = k^2 1+3+...+(2k1)=(1+2k1)k/2=k2
因此若 n < k 2 n < k^2 n<k2,则无解

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e3 + 5;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353;

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        ll n, k;
        scanf("%lld%lld", &n, &k);
        if((n - k) % 2 || n < k * k)    puts("NO");
        else                            puts("YES");
    }
    return 0;
}





目录

1327B-Princesses and Princes[思维]

time limit per testmemory limit per testinputoutput
2 seconds256 megabytesstandard inputstandard output

Description:

The King of Berland Polycarp LXXXIV has n n n daughters. To establish his power to the neighbouring kingdoms he wants to marry his daughters to the princes of these kingdoms. As a lucky coincidence there are n n n other kingdoms as well.

So Polycarp LXXXIV has enumerated his daughters from 1 1 1 to n n n and the kingdoms from 1 1 1 to n n n. For each daughter he has compiled a list of kingdoms princes of which she wanted to marry.

Polycarp LXXXIV is very busy, so he finds a couple for his daughters greedily one after another.

For the first daughter he takes the kingdom with the lowest number from her list and marries the daughter to their prince. For the second daughter he takes the kingdom with the lowest number from her list, prince of which hasn’t been taken already. If there are no free princes in the list then the daughter marries nobody and Polycarp LXXXIV proceeds to the next daughter. The process ends after the n n n-th daughter.

For example, let there be 4 4 4 daughters and kingdoms, the lists daughters have are [ 2 , 3 ] , [ 1 , 2 ] , [ 3 , 4 ] , [ 3 ] [2,3], [1,2], [3,4], [3] [2,3],[1,2],[3,4],[3], respectively.
在这里插入图片描述
In that case daughter 1 1 1 marries the prince of kingdom 2 2 2, daughter 2 2 2 marries the prince of kingdom 1 1 1, daughter 3 3 3 marries the prince of kingdom 3 3 3, leaving daughter 4 4 4 nobody to marry to.

Actually, before starting the marriage process Polycarp LXXXIV has the time to convince one of his daughters that some prince is also worth marrying to. Effectively, that means that he can add exactly one kingdom to exactly one of his daughter’s list. Note that this kingdom should not be present in the daughter’s list.

Polycarp LXXXIV wants to increase the number of married couples.

Unfortunately, what he doesn’t have the time for is determining what entry to add. If there is no way to increase the total number of married couples then output that the marriages are already optimal. Otherwise, find such an entry that the total number of married couples increases if Polycarp LXXXIV adds it.

If there are multiple ways to add an entry so that the total number of married couples increases then print any of them.

For your and our convenience you are asked to answer t t t independent test cases.

Input

The first line contains a single integer t ( 1 ≤ t ≤ 1 0 5 ) t (1≤t≤10^5) t(1t105) — the number of test cases.
Then t t t test cases follow.
The first line of each test case contains a single integer n ( 1 ≤ n ≤ 1 0 5 ) n (1≤n≤10^5) n(1n105) — the number of daughters and the number of kingdoms.
Each of the next n n n lines contains the description of each daughter’s list. The first integer k ( 0 ≤ k ≤ n ) k (0≤k≤n) k(0kn) is the number of entries in the i i i-th daughter’s list. After that k k k distinct integers follow g i [ 1 ] , g i [ 2 ] , … , g i [ k ] ( 1 ≤ g i [ j ] ≤ n ) g_i[1],g_i[2],…,g_i[k] (1≤g_i[j]≤n) gi[1],gi[2],,gi[k](1gi[j]n) — the indices of the kingdoms in the list in the increasing order ( g i [ 1 ] < g i [ 2 ] < ⋯ < g i [ k ] ) (g_i[1]<g_i[2]<⋯<g_i[k]) (gi[1]<gi[2]<<gi[k]).
It’s guaranteed that the total number of daughters over all test cases does not exceed 10^5.
It’s also guaranteed that the total number of kingdoms in lists over all test cases does not exceed 1 0 5 10^5 105.

Output

For each test case print the answer to it.

Print “IMPROVE” in the first line if Polycarp LXXXIV can add some kingdom to some of his daughter’s list so that the total number of married couples increases. The second line then should contain two integers — the index of the daughter and the index of the kingdom Polycarp LXXXIV should add to that daughter’s list.

If there are multiple ways to add an entry so that the total number of married couples increases then print any of them.

Otherwise the only line should contain one word “OPTIMAL”.

Example input

5
4
2 2 3
2 1 2
2 3 4
1 3
2
0
0
3
3 1 2 3
3 1 2 3
3 1 2 3
1
1 1
4
1 1
1 2
1 3
1 4

Example output

IMPROVE
4 4
IMPROVE
1 1
OPTIMAL
OPTIMAL
OPTIMAL

Note

The first test case is depicted in the statement. Adding the fourth kingdom to the list of the fourth daughter makes her marry the prince of the fourth kingdom.
In the second test case any new entry will increase the number of marriages from 0 0 0 to 1 1 1.
In the third and the fourth test cases there is no way to add an entry.
In the fifth test case there is no way to change the marriages by adding any entry.

分析:
题意:
n n n和女儿 n n n个王子,给每个女儿配个王子
如果无法再增加对数,则输出"OPTIMAL"
否则输出"IMPROVE",再输出增加对数的女儿编号和王子编号,只需要一对就可以了
做法:
对于每个当前女儿,找输入后可以配对的第一个王子标记
之后遍历一遍

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353;

int re() {
    int x = 0, f = 1; char c = getchar();
    while(c > '9' || c < '0'){if(c == '-')f=-f; c = getchar();}
    while(c >= '0' && c <= '9'){x=(x<<1)+(x<<3)+c-'0'; c = getchar();}
    return x * f;
}

vector<int> ed[maxn];
bool vis[2][maxn];

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int n, m, u, v;
        scanf("%d", &n);
        memset(vis, false, sizeof(vis));
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &m);
            while(m--) {
                scanf("%d", &v);
                ed[i].push_back(v);
                if(vis[0][i] || vis[1][v])  continue;
                vis[0][i] = vis[1][v] = true;
            }
        }
        int cnt = 0;
        for(int i = 1; i <= n; ++i)
            if(vis[0][i])   cnt++;
        if(cnt == n)
            puts("OPTIMAL");
        else {
            bool flag = false;
            for(int i = 1; i <= n; ++i) {
                if(vis[0][i])  continue;
                for(int j = 1; j <= n; ++j) {
                    if(vis[1][j])   continue;
                    u = i, v = j, flag = true;
                    break;
                }
                if(flag)    break;
            }
            printf("IMPROVE\n%d %d\n", u, v);
        }
    }
    return 0;
}





目录

1327C - Game with Chips[思维]

time limit per testmemory limit per testinputoutput
1 seconds256 megabytesstandard inputstandard output

Description:

Petya has a rectangular Board of size n × m n \times m n×m. Initially, k k k chips are placed on the board, i i i-th chip is located in the cell at the intersection of s x i sx_i sxi-th row and s y i sy_i syi-th column.

In one action, Petya can move all the chips to the left, right, down or up by 1 1 1 cell.

If the chip was in the ( x , y ) (x,y) (x,y) cell, then after the operation:
  left, its coordinates will be ( x , y − 1 ) (x,y−1) (x,y1);
  right, its coordinates will be ( x , y + 1 ) (x,y+1) (x,y+1);
  down, its coordinates will be ( x + 1 , y ) (x+1,y) (x+1,y);
  up, its coordinates will be ( x − 1 , y ) (x−1,y) (x1,y).
If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position.

Note that several chips can be located in the same cell.

For each chip, Petya chose the position which it should visit. Note that it’s not necessary for a chip to end up in this position.

Since Petya does not have a lot of free time, he is ready to do no more than 2 n m 2nm 2nm actions.

You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in 2 n m 2nm 2nm actions.

Input

The first line contains three integers n , m , k ( 1 ≤ n , m , k ≤ 200 ) n,m,k (1≤n,m,k≤200) n,m,k(1n,m,k200) — the number of rows and columns of the board and the number of chips, respectively.

The next k k k lines contains two integers each s x i , s y i ( 1 ≤ s x i ≤ n , 1 ≤ s y i ≤ m ) sx_i,sy_i (1≤sx_i≤n,1≤sy_i≤m) sxi,syi(1sxin,1syim) — the starting position of the i i i-th chip.

The next k k k lines contains two integers each f x i , f y i ( 1 ≤ f x i ≤ n , 1 ≤ f y i ≤ m ) fx_i,fy_i (1≤fx_i≤n,1≤fy_i≤m) fxi,fyi(1fxin,1fyim) — the position that the i i i-chip should visit at least once.

One Intput

3 3 2
1 2
2 1
3 3
3 2

One Output

3
DRD

Two Intput

5 4 3
3 4
3 1
3 3
5 3
1 3
1 4

Two Output

9
DDLUUUURR

分析:
题意:
U D L R UDLR UDLR分别表示上下左右移动一步
假如现在移动到最边界,再往边界方向移动位置不会变化
现在要使得任意 s i s_i si移动经过 f i f_i fi
问移动步数在 2 n m 2nm 2nm内的移动过程为怎样的

分析:
只要先全部移动到一个角落,然后蛇形走遍每个位置

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200 + 5;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353;

int re() {
    int x = 0, f = 1; char c = getchar();
    while(c > '9' || c < '0'){if(c == '-')f=-f; c = getchar();}
    while(c >= '0' && c <= '9'){x=(x<<1)+(x<<3)+c-'0'; c = getchar();}
    return x * f;
}

char s[maxn * maxn * 2];

int main() {
    int n, m, k, x, y;
    scanf("%d%d%d", &n, &m, &k); k = 2*k+1;
    for(int i = 1; i < k; ++i)  scanf("%d%d", &x, &y);
    k = 0; int flag = 0;
    for(int i = 1; i < m; ++i)  s[k++] = 'L';
    for(int i = 1; i < n; ++i)  s[k++] = 'U';
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j < m; ++j)
            s[k++] = flag ? 'L' : 'R';
        if(i != n)  s[k++] = 'D';
        flag ^= 1;
    }
    s[k] = 0;
    printf("%d\n%s\n", k, s);
    return 0;
}





目录

1327E - Count The Blocks[找规律]

time limit per testmemory limit per testinputoutput
2 seconds256 megabytesstandard inputstandard output

Description:

You wrote down all integers from 0 0 0 to 1 0 n − 1 10^n−1 10n1, padding them with leading zeroes so their lengths are exactly n n n. For example, if n = 3 n=3 n=3 then you wrote out 000 , 001 , . . . , 998 , 999 000, 001, ..., 998, 999 000,001,...,998,999.

A block in an integer x x x is a consecutive segment of equal digits that cannot be extended to the left or to the right.

For example, in the integer 00027734000 00027734000 00027734000 there are three blocks of length 1 1 1, one block of length 2 2 2 and two blocks of length 3 3 3.

For all integers i i i from 1 1 1 to n n n count the number of blocks of length i i i among the written down integers.

Since these integers may be too large, print them modulo 998244353 998244353 998244353.

One Input

1

One Output

10

Two Input

2

Two Output

180 10

分析:
题意:
长度为 n n n的数字串,例如 n = 3 n=3 n=3
如果当前数为 088 088 088,那么连续相同数字长度为 2 2 2的有一个" 88 88 88",为 1 1 1的有一个" 0 0 0"
最后遍历输出长度为 1 → n 1 \rightarrow n 1n分别出现了几次
分析:
遇事不决先打表
注释的部分是打表的
然后可以发现规律

10
180 10
2610 180 10
34200 2610 180 10
423000 34200 2610 180 10
5040000 423000 34200 2610 180 10

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const ll mod = 998244353;

int re() {
    int x = 0, f = 1; char c = getchar();
    while(c > '9' || c < '0'){if(c == '-')f=-f; c = getchar();}
    while(c >= '0' && c <= '9'){x=(x<<1)+(x<<3)+c-'0'; c = getchar();}
    return x * f;
}

ll a[maxn];

int main() {
    int n; ll x = 81;
    a[1] = 10, a[2] = 180;
    scanf("%d", &n);
    for(int i = 3; i <= n; ++i, x = 10 * x % mod)
        a[i] = (a[i - 1] + x) % mod * 10 % mod;
    for(int i = n; i >= 1; --i)
        printf("%lld%c", a[i], i == 1 ? '\n' : ' ');
    return 0;
}

/*
int num[maxn];

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        int x = 1;
        for(int j = 1; j <= i; ++j) x *= 10;
        x -= 1;
        cout << '\n' << x <<endl;
        memset(num, 0, sizeof(num));
        num[i] += 1;
        for(int j = 1; j <= x; ++j) {
            int y = j, z, cnt = 0, pre = -1, pos = 0;
            while(y) {
                z = y % 10; y /= 10;
                if(pos == 0 || pre == z)    cnt += 1, pre = z;
                else {
                    num[cnt] += 1;
                    cnt = 1, pre = z;
                }
                pos++;
            }
            num[cnt] += 1;
            if(pos != i)    num[i - pos] += 1;
        }
        printf("%d\n", i);
        for(int j = 1; j <= i; ++j)
            printf("%d%c", num[j], j == i ? '\n' : ' ');
    }
    return 0;
}

7

9
1
10

99
2
180 10

999
3
2610 180 10

9999
4
34200 2610 180 10

99999
5
423000 34200 2610 180 10

999999
6
5040000 423000 34200 2610 180 10

9999999
7
58500000 5040000 423000 34200 2610 180 10
*/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值