Description:
Ayoub thinks that he is a very smart person, so he created a function where s is a binary string (a string which contains only symbols and ). The function is equal to the number of substrings in the string that contains at least one symbol, that is equal to .
More formally, is equal to the number of pairs of integers , such that 1≤l≤r≤|s| (where is equal to the length of string s), such that at least one of the symbols is equal to
For example, if then because there are such pairs
Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers n and m and asked him this problem. For all binary strings s of length n which contains exactly m symbols equal to , find the maximum value of .
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 — the number of test cases. The description of the test cases follows.
The only line for each test case contains two integers — the length of the string and the number of symbols equal to in it.
Output
For every test case print one integer number — the maximum value of over all strings s of length , which has exactly m symbols, equal to .
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 strings of length , which has exactly 1 symbol, equal to . These strings are: The values of f for them are: , so the maximum value is and the answer is .
In the second test case, the string s with the maximum value is
In the third test case, the string with the maximum value is
In the fourth test case, the only string s of length 4, which has exactly symbols, equal to is and the value of f for that string is , so the answer is
In the fifth test case, the string s with the maximum value is and it is described as an example in the problem statement.
题意:
给吹一个长度为 的 串,里面有 个 ,可以任意选取区间,如果这个区间内至少含有一个 ,则得一分,问怎么排列可以使得得分最高,输出最高得分。
我们知道如果都是1的话我们可以选取 对,那么如果有连续的 那么假设 的长度为 那不能选取的对数就是 。将这些 划分为 个组,以便使每个组的 之和最小。
最优的方法是将它们分成相等的组或尽可能相等。
每组的长度就是 ,令
这些不能选取的区间总数就是
如果不能整除划分区间,就把多余的 分到每个区间内,这样加上这些 对每个区间 不能选取的对数就变成了 。全部的就是
用全部可选的减去不能选取的就是最后答案。
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
if (a > 9)
Out(a / 10);
putchar(a % 10 + '0');
}
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(ll a, ll b, ll mod)
{
if (a >= mod)
a = a % mod + mod;
ll ans = 1;
while (b)
{
if (b & 1)
{
ans = ans * a;
if (ans >= mod)
ans = ans % mod + mod;
}
a *= a;
if (a >= mod)
a = a % mod + mod;
b >>= 1;
}
return ans;
}
// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
return qpow(a, p - 2, p);
}
///扩展欧几里得
int exgcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int g = exgcd(b, a % b, x, y);
int t = x;
x = y;
y = t - a / b * y;
return g;
}
///使用ecgcd求a的逆元x
int mod_reverse(int a, int p)
{
int d, x, y;
d = exgcd(a, p, x, y);
if (d == 1)
return (x % p + p) % p;
else
return -1;
}
///中国剩余定理模板0
ll china(int a[], int b[], int n) //a[]为除数,b[]为余数
{
int M = 1, y, x = 0;
for (int i = 0; i < n; ++i) //算出它们累乘的结果
M *= a[i];
for (int i = 0; i < n; ++i)
{
int w = M / a[i];
int tx = 0;
int t = exgcd(w, a[i], tx, y); //计算逆元
x = (x + w * (b[i] / t) * x) % M;
}
return (x + M) % M;
}
ll n, m;
int t;
ll ans, res, tmp, cnt;
int main()
{
sd(t);
while (t--)
{
sldd(n, m);
ans = n * (n + 1) / 2;
ll z = n - m;
ll k = z / (m + 1);
ans -= (m + 1) *k * (k + 1) / 2;
ans -= (z % (m + 1)) * (k + 1);
pld(ans);
}
return 0;
}