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
5
3 1
3 2
3 3
4 0
5 2
Output
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.
这种思维题真的不能用等级来评定,会的人觉得它跟A一样的水平,不会的就是和F一样的水平。
思路:我们求含有1的字符串最大个数,那么就是求含有0的字符串最小个数。因为求1不是很好求。一共有(n+1)*n/2个子字符串,一共有m个1,所以一共有n-m个0。对这n-m个0,一共有m+1个位置可以放置。因为要求最小,所以每个位置的0的个数应当取平均值cnt1=(n-m)/(m+1)。但是呢,不一定整除,有可能会有cnt2=(n-m)%(m+1)个位置是cnt1+1个0的,所以我们最后再减去(cnt1+1) * (cnt2).最后答案等于(n+1)*n/2-(cnt1+1) * cnt1/2 *(m+1)-(cnt1+1) * (cnt2).
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;ll n,m;int main()
{int t;scanf("%d",&t);while(t--){cin>>n>>m;ll ans1=(n+1ll)*n/2ll;//总共的子字符串数ll cnt1=(n-m)/(m+1ll);ll cnt2=(n-m)%(m+1ll);cout<<ans1-(cnt1*(cnt1+1ll)/2ll)*(m+1ll)-(cnt1+1ll)*cnt2<<endl;}return 0;
}

努力加油a啊,(o)/~

Ayoub's function CodeForces - 1301C(组合数学)相关推荐

  1. codeforces数学1700[CodeForces 1336B[分类讨论+二分]CodeForces - 1301C[组合计数的减法原理]]

    CodeForces 1336B Xenia and Colorful Gems 题目大意:给你nrn_rnr​个xix_ixi​,ngn_gng​个yiy_iyi​,nbn_bnb​个ziz_izi ...

  2. CodeForces - 1301C Ayoub's function(数学)

    题目链接:点击查看 题目大意:规定函数 f(s) 为01字符串 s 中至少包含一个 1 的所有子串的数量,比如 f(10101) = 12 ,其中十二个子串分别为(1,2),(1,3),(1,4),( ...

  3. Anu Has a Function CodeForces - 1300C(二进制位运算)

    Anu has created her own function ff: f(x,y)=(x|y)−yf(x,y)=(x|y)−y where || denotes the bitwise OR op ...

  4. E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)

    http://codeforces.com/contest/862/problem/E 二分答案 一个数与数组中的哪个数最接近: 先对数组中的数排序,然后lower_bound 1 #include ...

  5. Codeforces Round #439 (Div. 2) Problem C (Codeforces 869C) - 组合数学

    - This is not playing but duty as allies of justice, Nii-chan! - Not allies but justice itself, Onii ...

  6. 寒假刷题13: Anu Has a Function Codeforces Round #618 (Div. 2) C

    题目链接: Anu Has a Function 题目解析: 观察函数f(x,y)定义:(x|y)-y 即 取出来x里是1但是y里不是1的地方 也就是 x&(~y) (也可以列真值表) 因此题 ...

  7. 2022年5月8号补题

    title: 5月8号补题 date: 2022-05-08 10:37:59 author: "胡耀文" categories: "算法" tags: &qu ...

  8. Codeforces Round #619 (Div. 2)

    文章目录 Codeforces Round #619 (Div. 2) B - Motarack's Birthday C - Ayoub's function E. Nanosoft Codefor ...

  9. AC. Anu Has a Function

    A&C. Anu Has a Function Codeforces Round #618 (Div. 1&2) 题目链接 关键词 greedy math *1500 位运算 解题思路 ...

最新文章

  1. 5G到底有多块?刚刚5G商用牌照正式发布,一共 4 张!
  2. 自己动手修改龙邱信标灯固件FM频率
  3. ruby File类
  4. UA SIE545 优化理论基础4 对偶理论简介2 弱对偶与Duality Gap
  5. 在Flutter中设置更好的Logging的指南
  6. 2020 BrandZ全球品牌价值排行榜100强发布 TikTok首次上榜
  7. mysql 无符号 负数_mysql下有符号数和无符号数的相关问题
  8. ccd视觉定位教程_CCD与CMOS哪个更能推动工业相机市场的发展
  9. easyui框架中关于dialog自带关闭事件的使用
  10. 服务器技术文件,服务端开发技术文档要包含什么?
  11. 找出数组中从未出现的最小正整数java实现
  12. 2020年最值得关注的28款区块链游戏
  13. c语言饭卡管理系统_C语言饭卡管理系统(附代码) -
  14. 520来袭,程序猿的浪漫 - 前端情书
  15. java代码混淆,程序加密推荐 java授权 支持JDK16
  16. DOS命令关机小程序
  17. 我被老板炒鱿鱼了!因为我在IDE里看漂亮小姐姐跳舞!(IntelliJ IDEA插件开发之打造炫酷动态背景墙)
  18. list集合练习----斗地主
  19. SSM+jsp整合步骤(注解开发)
  20. 在线json校验工具

热门文章

  1. 阿里云使用教程(试用一个月)
  2. python开发sqlite3完整_让Python更加充分的使用Sqlite3-阿里云开发者社区
  3. raspberrypi python传感器_Raspberry Pi和Arduino读取串行传感器d
  4. Android开发之Base64与bitmap互转的工具类
  5. 访问windows azure虚拟机iis服务器,如何直接从Java访问Azure/IIS证书?
  6. c++ 传入回调函数 参数没有传入_一文读懂回调函数
  7. 用计算机怎么录资料,如何在短时间内快速录入大量数据 -电脑资料
  8. “指定的文件名无效或太长” 无法拷贝,咋办
  9. 配置View桌面时找不到域的解决方法
  10. 1.8-samba 部署和优化-2