ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

codeforces 1301C Ayoub's function (排列组合)

2020-03-04 21:00:27  阅读:271  来源: 互联网

标签:function cnt number string contains equal codeforces symbols Ayoub


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 nnand 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

题意:给出01字符串的长度,告诉1的数量,求包含1区间的个数。
思路:先求出全部区间的数量,然后贪心的想用1把0全部平均分割开,然后减去只有0组合在一起的数量。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define PI 3.14159
#define ll long long
int t;
ll n, m;
int main()
{
    ll ans, cnt, tmp;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%lld%lld", &m, &n);
        ans = m * (m + 1) / 2;
        ans -= m - n;
        if (n == 0)
        {
            printf("0\n");
            continue;
        }
        else if (n >= m / 2) {
            printf("%lld\n", ans);
            continue;
        }
        m -= n;
        cnt = m / (n + 1);
        tmp = m % (n + 1);
        ans -= cnt * (cnt - 1) / 2 * (n + 1) + tmp * cnt;
        cout << ans << endl;
    }
}

 

标签:function,cnt,number,string,contains,equal,codeforces,symbols,Ayoub
来源: https://www.cnblogs.com/Tangent-1231/p/12416117.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有