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

探讨了如何构造一个长度为n的二进制字符串,其中包含m个'1',使得该字符串中至少包含一个'1'的子串数量最大化。通过将'n-m'个'0'平均分配到由'm'个'1'创建的段中,得出了一种计算最大子串计数的公式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Ayoub’s function
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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的01串,其中有m个1,现在让你构造一个01串s,让他的f(s)最大,f(s)为字符1的个数≥1的子串个数

思路:首先一个字符串的子串个数自然就是n*(n+1)/2,我们要让f(s)最大,其实也就是等价 把n-m个0 平均分成(m+1)份 因为有m个1,每个1可以当作隔板一样隔开。
那么我们算出来num=(n-m)/(m+1) 这就是平均分的每一部分的0的个数 对于r=(n-m)%(m+1)
当然是在m+1份里 每个放1即可
那么答案就是 n*(n+1)/2-r*(num+1)(m+1-r)(num+2)/2-num(num+1)/2

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define me(a,x) memset(a,x,sizeof a)
#define pb(a) push_back(a)
#define pa pair<int,int>
#define fi first
#define se second
int main()
{

    int t;
    cin>>t;
    while(t--)
    {
        ll n,m;cin>>n>>m;
        ll ans=n*(n+1)/2;
        ll num=(n-m)/(m+1);
        ll r=(n-m)%(m+1);
        cout<<ans-r*(num+1)*(num+2)/2-(m+1-r)*num*(num+1)/2<<endl;
    }
    return 0;
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不会c语言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值