CF1301C.Ayoub's function

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Ayoub thinks that he is a very smart person, so he created a function f(s)f(s), where ss is a binary string (a string which contains only symbols "0" and "1"). The function f(s)f(s) is equal to the number of substrings in the string ss that contains at least one symbol, that is equal to "1".

More formally, f(s)f(s) is equal to the number of pairs of integers (l,r)(l, r), such that 1≤l≤r≤∣s∣1 \leq l \leq r \leq |s| (where ∣s∣|s| is equal to the length of string ss), such that at least one of the symbols sl,sl+1,…,srs_l, s_{l+1}, \ldots, s_r is equal to "1".

For example, if $s = $"01010" then f(s)=12f(s) = 12, because there are 1212 such pairs (l,r)(l, r): (1,2),(1,3),(1,4),(1,5),(2,2),(2,3),(2,4),(2,5),(3,4),(3,5),(4,4),(4,5)(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5).

Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers nn and mm and asked him this problem. For all binary strings ss of length nn which contains exactly mm symbols equal to "1", find the maximum value of f(s)f(s).

Mahmoud couldn't solve the problem so he asked you for help. Can you help him?

Ayoub 认为自己非常聪明,因此他定义了一个函数 f(s)f(s),其中 ss 是一个二进制字符串(即仅由字符 "0" 和 "1" 组成的字符串)。函数 f(s)f(s) 的值等于字符串 ss 中至少包含一个 "1" 的子串的个数。

更准确地说,f(s)f(s) 等于满足如下条件的整数对 (l,r)(l, r) 的个数:1≤l≤r≤∣s∣1 \leq l \leq r \leq |s|(其中 ∣s∣|s| 表示字符串 ss 的长度),且子串 sl,sl+1,…,srs_l, s_{l+1}, \ldots, s_r 中至少有一个字符为 "1"。

例如,若 $s = "01010""01010",则 f(s)=12f(s) = 12,因为满足条件的 (l,r)(l, r) 共有 1212 对:(1,2),(1,3),(1,4),(1,5),(2,2),(2,3),(2,4),(2,5),(3,4),(3,5),(4,4),(4,5)(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)。

Ayoub 还认为自己比 Mahmoud 更聪明,于是他给了 Mahmoud 两个整数 nn 和 mm,并提出如下问题:在所有长度为 nn、且恰好包含 mm 个 "1" 的二进制字符串 ss 中,求 f(s)f(s) 的最大可能值。

Mahmoud 无法解决这个问题,于是向你求助。你能帮他吗?

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051 \leq t \leq 10^5) — the number of test cases. The description of the test cases follows.

The only line for each test case contains two integers nn, mm (1≤n≤1091 \leq n \leq 10^{9}, 0≤m≤n0 \leq m \leq n) — the length of the string and the number of symbols equal to "1" in it.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例仅有一行,包含两个整数 nn 和 mm(1≤n≤1091 \leq n \leq 10^{9},0≤m≤n0 \leq m \leq n),分别表示字符串的长度以及其中字符 "1" 的个数。

输出格式

For every test case print one integer number — the maximum value of f(s)f(s) over all strings ss of length nn, which has exactly mm symbols, equal to "1".

对于每个测试用例,输出一个整数——在所有长度为 nn 且恰好包含 mm 个字符 "1" 的字符串 ss 中,f(s)f(s) 的最大值。

输入输出样例

  • 输入#1

    5
    3 1
    3 2
    3 3
    4 0
    5 2

    输出#1

    4
    5
    6
    0
    12

说明/提示

In the first test case, there exists only 33 strings of length 33, which has exactly 11 symbol, equal to "1". These strings are: $s_1 = $"100", $s_2 = $"010", $s_3 = $"001". The values of ff for them are: f(s1)=3,f(s2)=4,f(s3)=3f(s_1) = 3, f(s_2) = 4, f(s_3) = 3, so the maximum value is 44 and the answer is 44.

In the second test case, the string ss with the maximum value is "101".

In the third test case, the string ss with the maximum value is "111".

In the fourth test case, the only string ss of length 44, which has exactly 00 symbols, equal to "1" is "0000" and the value of ff for that string is 00, so the answer is 00.

In the fifth test case, the string ss with the maximum value is "01010" and it is described as an example in the problem statement.

在第一个测试用例中,仅存在 33 个长度为 33、且恰好包含 11 个字符 "1" 的字符串。这些字符串为:$s_1 = $"100", $s_2 = $"010", $s_3 = $"001"。它们对应的 ff 值分别为:f(s1)=3,f(s2)=4,f(s3)=3f(s_1) = 3, f(s_2) = 4, f(s_3) = 3,因此最大值为 44,答案为 44。

在第二个测试用例中,使 ff 值最大的字符串 ss 是 "101"。

在第三个测试用例中,使 ff 值最大的字符串 ss 是 "111"。

在第四个测试用例中,唯一一个长度为 44、且恰好包含 00 个字符 "1" 的字符串 ss 是 "0000",该字符串对应的 ff 值为 00,因此答案为 00。

在第五个测试用例中,使 ff 值最大的字符串 ss 是 "01010",该字符串已在题目描述中作为示例给出。

输入解题思路,AI测评打分。不知道怎么写?

首页