C. Ayoub's function---------------------逆向思维

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

More formally, f(s) is equal to the number of pairs of integers (l,r), such that 1≤l≤r≤|s| (where |s| is equal to the length of string s), such that at least one of the symbols sl,sl+1,…,sr is equal to “1”.

For example, if s=“01010” then f(s)=12, because there are 12 such pairs (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).

Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers n and m and asked him this problem. For all binary strings s of length n which contains exactly m symbols equal to “1”, find the maximum value of 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 t (1≤t≤105) — the number of test cases. The description of the test cases follows.

The only line for each test case contains two integers n, m (1≤n≤109, 0≤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) over all strings s of length n, which has exactly m symbols, equal to “1”.

Example

inputCopy
5
3 1
3 2
3 3
4 0
5 2
outputCopy
4
5
6
0
12
Note
In the first test case, there exists only 3 strings of length 3, which has exactly 1 symbol, equal to “1”. These strings are: s1=“100”, s2=“010”, s3=“001”. The values of f for them are: f(s1)=3,f(s2)=4,f(s3)=3, so the maximum value is 4 and the answer is 4.

In the second test case, the string s with the maximum value is “101”.

In the third test case, the string s with the maximum value is “111”.

In the fourth test case, the only string s of length 4, which has exactly 0 symbols, equal to “1” is “0000” and the value of f for that string is 0, so the answer is 0.

In the fifth test case, the string s with the maximum value is “01010” and it is described as an example in the problem statement.
题意:
给出一个字符串的长度为n,且里面有m个1,问你[l,r]区间内至少包含1个‘1’的个数

解析:

'1’可能太多,我们可以逆向思维去求0的个数,总的个数-包含0的个数==包含1的个数

因为我们要使包含1的个数多,那么包含0的个数一定要小,所以包含0的段分的越多越好。
我们会分成m+1段,每段有a=(n-m)/(m+1).但有时候有的段会存在a+1个0,所以
b=(n-m)%m 判断有没有这种块。如果有 那么我们一共有(n-m-b)个a的块,有
b个(a+1)的块
详细看代码代码有备注

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
int t;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d %d",&n,&m);
		ll ans=1ll*(n+1)*n/2;//总的个数 
		n=n-m;//剩余0得个数
		m=m+1;//会给0分成m+1段
		ll a=n/m;//每一段0得个数
		ll b=n%m;//会有b段的个数为a+1
		ans=ans-(a+1)*a/2*(m-b)-b*(a+1+1)*(a+1)/2;
		cout<<ans<<endl; 
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值