Codeforces Round #619 (DIv.2) C Ayoub's function

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≤l≤r≤|s| (where |s||s| is equal to the length of string ss), such that at least one of the symbols sl,sl+1,…,srsl,sl+1,…,sr is equal to “1”.

For example, if s=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?

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051≤t≤105) — 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≤n≤109, 0≤m≤n0≤m≤n) — the length of the string and the number of symbols equal to “1” in it.

Output

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”.

Example

input

Copy

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

Copy

4
5
6
0
12
Note

In the first test case, there exists only 33 strings of length 33, which has exactly 11 symbol, equal to “1”. These strings are: s1=s1=“100”, s2=s2=“010”, s3=s3=“001”. The values of ff for them are: f(s1)=3,f(s2)=4,f(s3)=3f(s1)=3,f(s2)=4,f(s3)=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.

题解:n个0,m个1,则答案=总共子串数-不含1的子串数
f(n)=(n+1)*n/2 不含1的子串数等于各个0子串的长度之和,若0子串长度x,则(x+1)*x/2之和最小, 把0划分为m+1组,尽可能地使每组长度均衡可以使各组子串个数之和最小,即(n-m)/m+1为均衡的长度,个数是(m+1-(n-m)%(m+1)),而剩余的(n-m)%(m+1)组则选择k+1个0,这样一来,可使结果最大。

代码如下

#include<bits/stdc++.h>
using namespace std;
long long t,n,m,l,r,x,s,k;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		k=(n-m)/(m+1);
		l=(n-m)%(m+1);
		s=((n+1)*n/2)-((k+1)*k/2)*(m+1-l)-l*(k+1)*(k+2)/2;
		printf("%d\n",s);
	}
	return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值