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

Input

5
3 1
3 2
3 3
4 0
5 2

Output

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==0)
        {
            cout<<0<<endl;
            continue;
        }
        if(n==m)
        {
            cout<<n*(n+1)/2<<endl;
            continue;
        }
        long long ans=(n+1)*n/2;
        long long noz=n-m;
        if(noz<=m)
        {
            ans-=noz;
            cout<<ans<<endl;
            continue;
        }
        long long num =noz/(m+1);//每部分0的个数
        long long res_group=noz%(m+1);
        ans=ans-(m+1-res_group)*num*(num+1)/2-res_group*(num+1)*(num+2)/2;
        printf("%lld\n",ans);
    }
    return 0;
}

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

原文地址:https://www.cnblogs.com/lipoicyclic/p/12311936.html

时间: 2024-08-30 04:33:42

Codeforces Round #619 (Div. 2) Ayoub's function的相关文章

Codeforces Round #619 (Div. 2)

A. Three Strings 题意:给三个长度相同的非空字符串abc,依次将c中的每个字符和a或者b中对应位置的字符进行交换,交换必须进行,问能否使得ab相同. 思路:对于每一个位置,如果三个字符都不相同,那一定不同,如果有两个相同且不是ab相同,则合法,否则不合法,如果三个字符都相同,那么合法. 1 #include<bits/stdc++.h> 2 #define LL long long 3 #define dl double 4 void rd(int &x){ 5 x=0

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 + .. + ( - 1)nn Your task is to calculate f(n) for a given integer n. Input The single line contains the positive integer n (1 ≤ n ≤ 1015). Output Print f

codeforces水题100道 第十题 Codeforces Round #277 (Div. 2) A. Calculating Function (math)

题目链接:www.codeforces.com/problemset/problem/486/A题意:求表达式f(n)的值.(f(n)的表述见题目)C++代码: #include <iostream> using namespace std; long long f(long long n) { if (n % 2 == 0) return n / 2; else return n / 2 - n; } int main() { long long n; cin >> n; cou

Codeforces Round #619 (Div. 2) 简要题解

A:只要每个位置都满足a[i] = c[i]或b[i] = c[i]即可. int main() { int t; scanf("%d", &t); while(t --) { char a[110], b[110], c[110]; scanf("%s%s%s", a, b, c); int n = strlen(a), tag = 1; for(int i = 0; i < n; i ++) { if(a[i] == c[i] || b[i] ==

Codeforces Round #619 (Div. 2)C(构造,容斥)

1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int main(){ 5 ios::sync_with_stdio(false); 6 cin.tie(NULL); 7 cout.tie(NULL); 8 int t; 9 cin>>t; 10 while(t--){ 11 long long n,m; 12 cin>>n>>m; 13 lo

【codeforces】Codeforces Round #277 (Div. 2) 解读

门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; typedef long long LL ; LL n ; int main () { while ( ~scanf ( "%I64d" , &n )

Codeforces Round #245 (Div. 1)——Tricky Function

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)

/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #include <bits/stdc++.h> using namespace std; const int N = 2e5+5; int a[N], b[N], c[N], n; int aa[N], bb[N]; bool cmp1(int x, int y) { return a[x] > a[y

Codeforces Round #262 (Div. 2) 460B. Little Dima and Equation(枚举)

题目链接:http://codeforces.com/problemset/problem/460/B B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Little Dima misbehaved during a math lesson a lot and the nas