Codeforces Round #619 (Div. 2) 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
5
3 1
3 2
3 3
4 0
5 2
4
5
6
0
12 思维题,脑子不好使卡了我半天...问的是如何排列01串里m个1的位置是含有1的子串个数最大。可以逆向思维,用所有子串个数减去全为0的字符串的子串个数。而因为有关系:一个长为k的字符串的子串个数有k(k+1)/2个,故只需要排列好全0字符串即可。
分类讨论:当没有0时或者全为0时答案易得;当1的个数大于等于0的个数时,可以用1把每个0分隔开,这样能保证答案最大,(因为两个单独的0比连续的00所含子串个数少);当1的个数少于0的个数时,1要尽可能把0分开,m个1最多能分成m+1份,而必须要让0尽可能平均开,所以m+1不能整除n-m(0的个数)的话,要把余数拆成1,尽可能分给别的组,减的时候分别减一下即可。记得开long long。
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long t;
cin>>t;
while(t--)
{
long long n,m;
scanf("%lld%lld",&n,&m);
if(m==)
{
cout<<<<endl;
continue;
}
if(n==m)
{
cout<<n*(n+)/<<endl;
continue;
}
long long ans=(n+)*n/;
long long noz=n-m;
if(noz<=m)
{
ans-=noz;
cout<<ans<<endl;
continue;
}
long long num =noz/(m+);//每部分0的个数
long long res_group=noz%(m+);
ans=ans-(m+-res_group)*num*(num+)/-res_group*(num+)*(num+)/;
printf("%lld\n",ans);
}
return ;
}
Codeforces Round #619 (Div. 2) Ayoub's function的更多相关文章
- Codeforces Round #619 (Div. 2) A~D题解
最近网课也开始了,牛客上一堆比赛题目也没补,所以就D题后面的也懒得补了 A.Three String 水题 #include <cstdio> #include <cstring&g ...
- Codeforces Round #619 (Div. 2)
A. Three Strings 题意:给三个长度相同的非空字符串abc,依次将c中的每个字符和a或者b中对应位置的字符进行交换,交换必须进行,问能否使得ab相同. 思路:对于每一个位置,如果三个字符 ...
- Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对
D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...
- Codeforces Round #277 (Div. 2) A. Calculating Function 水题
A. Calculating Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/4 ...
- codeforces水题100道 第十题 Codeforces Round #277 (Div. 2) A. Calculating Function (math)
题目链接:www.codeforces.com/problemset/problem/486/A题意:求表达式f(n)的值.(f(n)的表述见题目)C++代码: #include <iostre ...
- Codeforces Round #277 (Div. 2)---A. Calculating Function (规律)
Calculating Function time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #277 (Div. 2)A. Calculating Function 水
A. Calculating Function For a positive integer n let's define a function f: f(n) = - 1 + 2 - 3 + ...
- Codeforces Round #619 (Div. 2)E思维+二维RMQ
题:https://codeforces.com/contest/1301/problem/E 题意:给个n*m的图形,q个询问,每次询问问询问区间最大的合法logo的面积是多少 分析:由于logo是 ...
- Codeforces Round #619 (Div. 2)D(模拟)
先把一种最长路线记录下来,根据k的大小存到ans中相应的答案再输出 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using n ...
随机推荐
- 【13】堆排序 最小K个数
题目 输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7.3.8这8个数字,则最小的4个数字是1.2.3.4. 收获 优先队列实现 (n1,n2)->n2-n1是 ...
- 【Python】 数字求和
# 用户输入数字 num1 = input('输入第一个数字:') num2 = input('输入第二个数字:') # 求和 sum = float(num1) + float(num2) # 显示 ...
- 【C语言】已知三角形三边长,求三角形面积
一. 数学基础: 已知三角形的三边,计算三角形面积,需要用到海伦公式: 即p=(a+b+c)/2 二. 算法: 输入三个边长,套用海伦公式计算面积,并输出. 可以先判断是否可以构成三角形,即任意两边之 ...
- [Note]Splay学习笔记
开个坑记录一下学习Splay的历程. Code 感谢rqy巨佬的代码,让我意识到了Splay可以有多短,以及我之前的Splay有多么的丑... int fa[N], ch[N][2], rev[N], ...
- springboot1.5.9 整合单机版redis3.2.8
redis是一种可基于内存也可基于持久话的日志型.key-value数据库.因为性能高,存储数据类型丰富等优势常被用作数据缓存. 我们利用spring-boot-autoconfiguration.j ...
- C语言实例-大小写字母间的转换
初学C语言都会遇到要求写大小写转换的题目 这类题目主要通过ASCII(美国信息交换标准代码)码差值实现,A对应ASCII码十进制数字是65,a对应ASCII码十进制数字是97,即大小写字母之间ASCI ...
- flask 2 进阶
# 创建项目 jinja2 语法基础 # pycharm 里面 创建 new project -->pure python 之后选择路径 选择解释器 以及虚拟环境问题 from flask im ...
- python浅析模块,包及其相关用法
一,模块 什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里面,代码会越来越长,越来越不容易维护. 为了编写可以维护的代码,我们把很多函数分组,分别放到不同额文件,这样,每个文 ...
- Mysql与PostgreSql数据库学习笔记
mysql 从最基础的数据引擎,到进程结构,都不能支持数据版本.导致其职能阻塞“并发”,不支持最基本的事务,innodb达不到基本事务要求,任何写数据,都导致整个表锁住.充其量只能算是一个玩具,或者说 ...
- swift中的可选类型
可选类型也是Swift语言新添加的对象.主要是为了解决对象变量或常量为空的情况.在前面定义的变量和常量都不能为空.里面必须要有值. Swift中的可选类型则允许变量(常量)中没有值(被设为nil).要 ...