Codeforces 1301 C . Ayoub's function(容斥+贪心)

Description:

Ayoub thinks that he is a very smart person, so he created a function f ( s ) , f(s), f(s), where s is a binary string (a string which contains only symbols " 0 " "0" "0" and " 1 " "1" "1"). The function f ( s ) f(s) f(s) is equal to the number of substrings in the string s s s that contains at least one symbol, that is equal to " 1 " "1" "1".

More formally, f ( s ) f(s) f(s) is equal to the number of pairs of integers ( l , r ) (l,r) (l,r), such that 1≤l≤r≤|s| (where ∣ s ∣ |s| s is equal to the length of string s), such that at least one of the symbols s l , s l + 1 , … , s r s_{l},s_{l+1},…,s_{r} sl,sl+1,,sr is equal to " 1 " . "1". "1".

For example, if s = " 01010 " s="01010" s="01010" then f ( s ) = 12 , f(s)=12, f(s)=12, because there are 12 12 12 such pairs ( 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 ) . (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). (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).

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 " 1 " "1" "1", find the maximum value of f ( s ) 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 t ( 1 ≤ t ≤ 1 0 5 ) t (1≤t≤10^5) t(1t105) — the number of test cases. The description of the test cases follows.

The only line for each test case contains two integers n , m ( 1 ≤ n ≤ 1 0 9 , 0 ≤ m ≤ n ) n, m (1≤n≤10^9, 0≤m≤n) n,m(1n109,0mn) — the length of the string and the number of symbols equal to " 1 " "1" "1" in it.

Output

For every test case print one integer number — the maximum value of f ( s ) f(s) f(s) over all strings s of length n n n, which has exactly m symbols, equal to " 1 " "1" "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 3 3 3 strings of length 3 3 3, which has exactly 1 symbol, equal to " 1 " "1" "1". These strings are: s 1 = " 100 " , s 2 = " 010 " , s 3 = " 001 " . s_{1}="100", s_{2}="010", s_{3}="001". s1="100",s2="010",s3="001". The values of f for them are: f ( s 1 ) = 3 , f ( s 2 ) = 4 , f ( s 3 ) = 3 f(s_{1})=3,f(s_{2})=4,f(s_{3})=3 f(s1)=3,f(s2)=4,f(s3)=3, so the maximum value is 4 4 4 and the answer is 4 4 4.

In the second test case, the string s with the maximum value is " 101 " . "101". "101".

In the third test case, the string s s s with the maximum value is " 111 " . "111". "111".

In the fourth test case, the only string s of length 4, which has exactly 0 0 0 symbols, equal to " 1 " "1" "1" is " 0000 " "0000" "0000" and the value of f for that string is 0 0 0, so the answer is 0. 0. 0.

In the fifth test case, the string s with the maximum value is " 01010 " "01010" "01010" and it is described as an example in the problem statement.

题意:

给吹一个长度为 n n n 01 01 01 串,里面有 m m m 1 1 1 ,可以任意选取区间,如果这个区间内至少含有一个 1 1 1 ,则得一分,问怎么排列可以使得得分最高,输出最高得分。

我们知道如果都是1的话我们可以选取 n ∗ ( n + 1 ) 2 \frac{n*(n+1)}{2} 2n(n+1) 对,那么如果有连续的 0 0 0 那么假设 0 0 0 的长度为 l l l 那不能选取的对数就是 l ∗ ( l + 1 ) 2 \frac{l*(l+1)}{2} 2l(l+1) 。将这些 0 0 0 划分为 m + 1 m + 1 m+1 个组,以便使每个组的 l ∗ ( l + 1 ) 2 \frac{l*(l+1)}{2} 2l(l+1) 之和最小。

最优的方法是将它们分成相等的组或尽可能相等。

每组的长度就是 n − m m + 1 \frac{n-m}{m + 1} m+1nm ,令 k = n − m m + 1 k=\frac{n-m}{m + 1} k=m+1nm

这些不能选取的区间总数就是 ( m + 1 ) ∗ k ∗ ( k + 1 ) 2 (m + 1) *\frac{k * (k + 1)}{2} (m+1)2k(k+1)

如果不能整除划分区间,就把多余的 0 0 0 分到每个区间内,这样加上这些 0 0 0 对每个区间 不能选取的对数就变成了 k + 1 k+1 k+1 。全部的就是 ( z % ( m + 1 ) ) ∗ ( k + 1 ) (z \% (m + 1)) * (k + 1) (z%(m+1))(k+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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值