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

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
");
            continue;
        }
        else if (n >= m / 2) {
            printf("%lld
", ans);
            continue;
        }
        m -= n;
        cnt = m / (n + 1);
        tmp = m % (n + 1);
        ans -= cnt * (cnt - 1) / 2 * (n + 1) + tmp * cnt;
        cout << ans << endl;
    }
}
原文地址:https://www.cnblogs.com/Tangent-1231/p/12416117.html