Codeforces Round 958 (Div. 2)

##A. Split the Multiset

A multiset is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. For example, { 2 , 2 , 4 } \{2,2,4\} {2,2,4} is a multiset.

You have a multiset S S S. Initially, the multiset contains only one positive integer n n n. That is, S = { n } S=\{n\} S={n}. Additionally, there is a given positive integer k k k.

In one operation, you can select any positive integer u u u in S S S and remove one copy of u u u from S S S. Then, insert no more than k k k positive integers into S S S so that the sum of all inserted integers is equal to u u u.

Find the minimum number of operations to make S S S contain n n n ones.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000). Description of the test cases follows.

The only line of each testcase contains two integers n , k n,k n,k ( 1 ≤ n ≤ 1000 , 2 ≤ k ≤ 1000 1\le n\le 1000,2\le k\le 1000 1n1000,2k1000).

Output

For each testcase, print one integer, which is the required answer.
Example
input
4
1 5
5 2
6 3
16 4
output
0
4
3
5

Note

For the first test case, initially S = { 1 } S=\{1\} S={1}, already satisfying the requirement. Therefore, we need zero operations.

For the second test case, initially S = { 5 } S=\{5\} S={5}. We can apply the following operations:

  • Select u = 5 u=5 u=5, remove u u u from S S S, and insert 2 , 3 2,3 2,3 into S S S. Now, S = { 2 , 3 } S=\{2,3\} S={2,3}.
  • Select u = 2 u=2 u=2, remove u u u from S S S, and insert 1 , 1 1,1 1,1 into S S S. Now, S = { 1 , 1 , 3 } S=\{1,1,3\} S={1,1,3}.
  • Select u = 3 u=3 u=3, remove u u u from S S S, and insert 1 , 2 1,2 1,2 into S S S. Now, S = { 1 , 1 , 1 , 2 } S=\{1,1,1,2\} S={1,1,1,2}.
  • Select u = 2 u=2 u=2, remove u u u from S S S, and insert 1 , 1 1,1 1,1 into S S S. Now, S = { 1 , 1 , 1 , 1 , 1 } S=\{1,1,1,1,1\} S={1,1,1,1,1}.

Using 4 4 4 operations in total, we achieve the goal.

For the third test case, initially S = { 6 } S=\{6\} S={6}. We can apply the following operations:

  • Select u = 6 u=6 u=6, remove u u u from S S S, and insert 1 , 2 , 3 1,2,3 1,2,3 into S S S. Now, S = { 1 , 2 , 3 } S=\{1,2,3\} S={1,2,3}.
  • Select u = 2 u=2 u=2, remove u u u from S S S, and insert 1 , 1 1,1 1,1 into S S S. Now, S = { 1 , 1 , 1 , 3 } S=\{1,1,1,3\} S={1,1,1,3}.
  • Select u = 3 u=3 u=3, remove u u u from S S S, and insert 1 , 1 , 1 1,1,1 1,1,1 into S S S. Now, S = { 1 , 1 , 1 , 1 , 1 , 1 } S=\{1,1,1,1,1,1\} S={1,1,1,1,1,1}.

Using 3 3 3 operations in total, we achieve the goal.

For the fourth test case, initially S = { 16 } S=\{16\} S={16}. We can apply the following operations:

  • Select u = 16 u=16 u=16, remove u u u from S S S, and insert 4 , 4 , 4 , 4 4,4,4,4 4,4,4,4 into S S S. Now, S = { 4 , 4 , 4 , 4 } S=\{4,4,4,4\} S={4,4,4,4}.
  • Repeat for 4 4 4 times: select u = 4 u=4 u=4, remove u u u from S S S, and insert 1 , 1 , 1 , 1 1,1,1,1 1,1,1,1 into S S S.

Using 5 5 5 operations in total, we achieve the goal.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    int cnt = 0;
    while (t--)
    {   
        int n, k;
        cin >> n >> k;
        if (n == 1)
        {
            cout << "0" << '\n';
            continue;//跳出本次,刚开始这里没写!!!
        }
        cnt = 0;
        while (n > k)
        {
            n = n - k + 1;
            cnt++;
        }   
        cout << cnt + 1 << '\n';
    }
    return 0;
}

##B. Make Majority

You are given a sequence [ a 1 , … , a n ] [a_1,\ldots,a_n] [a1,,an] where each element a i a_i ai is either 0 0 0 or 1 1 1. You can apply several (possibly zero) operations to the sequence. In each operation, you select two integers 1 ≤ l ≤ r ≤ ∣ a ∣ 1\le l\le r\le |a| 1lra (where ∣ a ∣ |a| a is the current length of a a a) and replace [ a l , … , a r ] [a_l,\ldots,a_r] [al,,ar] with a single element x x x, where x x x is the majority of [ a l , … , a r ] [a_l,\ldots,a_r] [al,,ar].

Here, the majority of a sequence consisting of 0 0 0 and 1 1 1 is defined as follows: suppose there are c 0 c_0 c0 zeros and c 1 c_1 c1 ones in the sequence, respectively.

  • If c 0 ≥ c 1 c_0\ge c_1 c0c1, the majority is 0 0 0.
  • If c 0 < c 1 c_0<c_1 c0<c1, the majority is 1 1 1.

For example, suppose a = [ 1 , 0 , 0 , 0 , 1 , 1 ] a=[1,0,0,0,1,1] a=[1,0,0,0,1,1]. If we select l = 1 , r = 2 l=1,r=2 l=1,r=2, the resulting sequence will be [ 0 , 0 , 0 , 1 , 1 ] [0,0,0,1,1] [0,0,0,1,1]. If we select l = 4 , r = 6 l=4,r=6 l=4,r=6, the resulting sequence will be [ 1 , 0 , 0 , 1 ] [1,0,0,1] [1,0,0,1].

Determine if you can make a = [ 1 ] a=[1] a=[1] with a finite number of operations.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 4 ⋅ 1 0 4 1 \le t \le 4\cdot 10^4 1t4104). Description of the test cases follows.

The first line of each testcase contains one integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1\le n\le 2\cdot 10^5 1n2105).

The second line of each testcase contains a string consisting of 0 0 0 and 1 1 1, describing the sequence a a a.

It’s guaranteed that the sum of n n n over all testcases does not exceed 2 ⋅ 1 0 5 2\cdot 10^5 2105.

Output

For each testcase, if it’s possible to make a = [ 1 ] a=[1] a=[1], print YES. Otherwise, print NO. You can output the answer in any case (upper or lower). For example, the strings yEs, yes, Yes, and YES will be recognized as positive responses.
Example
input
5
1
0
1
1
2
01
9
100000001
9
000011000
output
No
Yes
No
Yes
No

Note

In the fourth testcase of the example, initially a = [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 ] a=[1,0,0,0,0,0,0,0,1] a=[1,0,0,0,0,0,0,0,1]. A valid sequence of operations is:

  1. Select l = 2 , r = 8 l=2,r=8 l=2,r=8 and apply the operation. a a a becomes [ 1 , 0 , 1 ] [1,0,1] [1,0,1].
  2. Select l = 1 , r = 3 l=1,r=3 l=1,r=3 and apply the operation. a a a becomes [ 1 ] [1] [1].
    思路:因为选定的区域都变为一个数,所以连续的0可以记为一个,然后比较0,1的多少
#include <bits/stdc++.h>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int sum0 = 0, sum1 = 0;
		int n;
		cin >> n;
		string s;
		cin >> s;
		s = ' ' + s;
		for (int i = 1; i <= n; i++) {
			if (s[i] != s[i + 1] && s[i] == '0')//连续的0可以变为一个;
				sum0++;
			else if (s[i] == '1')
				sum1++;
		}
		if (sum1 > sum0)//这个时候只有1多就能最终是1
			cout << "Yes" << '\n';
		else
			cout << "No" << '\n';
	}
	return 0;
}

##C. Increasing Sequence with Fixed OR

You are given a positive integer n n n. Find the longest sequence of positive integers a = [ a 1 , a 2 , … , a k ] a=[a_1,a_2,\ldots,a_k] a=[a1,a2,,ak] that satisfies the following conditions, and print the sequence:

  • a i ≤ n a_i\le n ain for all 1 ≤ i ≤ k 1\le i\le k 1ik.
  • a a a is strictly increasing. That is, a i > a i − 1 a_i>a_{i-1} ai>ai1 for all 2 ≤ i ≤ k 2\le i\le k 2ik.
  • a i   ∣   a i − 1 = n a_i\,|\,a_{i-1}=n aiai1=n for all 2 ≤ i ≤ k 2\le i\le k 2ik, where ∣ | denotes the bitwise OR operation.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000). Description of the test cases follows.

The only line of each test case contains one integer n n n ( 1 ≤ n ≤ 1 0 18 1\le n\le 10^{18} 1n1018).

It’s guaranteed that the sum of lengths of the longest valid sequences does not exceed 5 ⋅ 1 0 5 5\cdot 10^5 5105.

Output

For each testcase, print two lines. In the first line, print the length of your constructed sequence, k k k. In the second line, print k k k positive integers, denoting the sequence. If there are multiple longest sequences, you can print any of them.
Example
input
4
1
3
14
23
output
1
1
3
1 2 3
4
4 10 12 14
5
7 18 21 22 23
思路:位运算,用n减掉二进制表示的n位置上是1所代表的值,所得的值就是所求的值,从小到大,包括本身

#include <iostream>
using namespace std;
int ans[105], t;

int main() {
	int T;
	cin >> T;
	while (T--) {
		long long n;
		scanf("%lld", &n);
		t = 0;
		for (int i = 0; (1LL << i) <= n; ++i)
			if ((1LL << i)&n )
				ans[++t] = i;

		if (t == 1) {
			printf("1\n");
			printf("%lld\n", n);
		} else {
			printf("%d\n", t + 1);
			for (int i = t; i >= 1; --i)
				printf("%lld ", n - (1LL << ans[i]));
			printf("%lld\n", n);
		}
	}
	return 0;
}

  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值