前置扯淡
这个是一道需要脑子的容斥 \(+\) 推式子的简单题
但是我现场就是做不出来……(卡题送自闭)
Description
题意:
给一个\(01\)串的长度和\(1\)的个数,求一种摆放方式,让含有\(1\)的子串个数最多
Solution
首先考虑容斥
含有 \(1\) 的子串个数 \(=\) 总个数 \(-\) 全 \(0\) 子串个数
一种相当显然的做法就是:
把所有\(1\)的间隔中放上等量的\(0\),然后就可以了
注意一下余数的细节就行了
不知道为啥比赛就是会写挂………(已经连续卡C自闭了……)
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace yspm{
inline int read()
{
int res=0,f=1; char k;
while(!isdigit(k=getchar())) if(k=='-') f=-1;
while(isdigit(k)) res=res*10+k-'0',k=getchar();
return res*f;
}
inline void work()
{
int n=read(),m=read();
int tot=(n+1)*n/2;
int z=n-m,g=m+1;
int k=(z/g);
cout<<tot-k*(k+1)/2*g-(k+1)*(z%g)<<endl;;
return ;
}
signed main()
{
int T=read(); while(T--) work();
return 0;
}
}
signed main(){return yspm::main();}