Codeforces #662 (Div. 2) C. Pinkie Pie Eats Patty-cakes(二分)

C. Pinkie Pie Eats Patty-cakes (二分)

原题链接:https://codeforces.com/contest/1393/problem/C

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Pinkie Pie has bought a bag of patty-cakes with different fillings! But it appeared that not all patty-cakes differ from one another with filling. In other words, the bag contains some patty-cakes with the same filling.

Pinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the patty-cakes but to try not to eat the patty-cakes with the same filling way too often. To achieve this she wants the minimum distance between the eaten with the same filling to be the largest possible. Herein Pinkie Pie called the distance between two patty-cakes the number of eaten patty-cakes strictly between them.

Pinkie Pie can eat the patty-cakes in any order. She is impatient about eating all the patty-cakes up so she asks you to help her to count the greatest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating!

Pinkie Pie is going to buy more bags of patty-cakes so she asks you to solve this problem for several bags!

Input
The first line contains a single integer T T T ( 1 ≤ T ≤ 100 ) : (1≤T≤100): (1T100): the number of bags for which you need to solve the problem.

The first line of each bag description contains a single integer n n n ( 2 ≤ n ≤ 105 ) : (2≤n≤105): (2n105): the number of patty-cakes in it. The second line of the bag description contains n n n integers a 1 , a 2 , … , a n a1,a2,…,an a1,a2,,an ( 1 ≤ a i ≤ n ) : (1≤ai≤n): (1ain): the information of patty-cakes’ fillings: same fillings are defined as same integers, different fillings are defined as different integers. It is guaranteed that each bag contains at least two patty-cakes with the same filling.

It is guaranteed that the sum of n n n over all bags does not exceed 1 0 5 10^5 105.

Output
For each bag print in separate line one single integer: the largest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating for that bag.

Example
input

4
7
1 7 1 6 4 4 6
8
1 1 4 6 4 6 4 7
3
3 3 3
6
2 5 2 3 1 4

output

3
2
0
4

Note
For the first bag Pinkie Pie can eat the patty-cakes in the following order ( ( (by fillings ) ) ) : 1 , 6 , 4 , 7 , 1 , 6 , 4 : 1, 6, 4, 7, 1, 6, 4 :1,6,4,7,1,6,4 ( ( (in this way, the minimum distance is equal to 3 ) ) ).

For the second bag Pinkie Pie can eat the patty-cakes in the following order ( ( (by fillings ) : ): ): 1 , 4 , 6 , 7 , 4 , 1 , 6 , 4 1, 4, 6, 7, 4, 1, 6, 4 1,4,6,7,4,1,6,4 ( ( (in this way, the minimum distance is equal to 2 ) 2) 2).
题意
题解
(先把代码贴上来,题解稍后补)

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#define int long long 
typedef long long ll;

const int N = 2e5 + 5;
const int M = 2020;
const int mod = 998244353;
using namespace std;

int len, a[N];
int n;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll quick_power_mod(ll a, ll b, ll m)//pow(a,b)%m
{
    a %= m;
    ll result = 1;
    while (b > 0)
    {
        if (b & 1)
            result = (result * a) % m;
        a = (a * a) % m;
        b >>= 1;
    }
    return result;
}
//计算组合数取模
ll comp(ll a, ll b, int p)//composite num C(a,b)%p
{
    if (a < b)return 0;
    if (a == b)return 1;
    if (b > a - b)b = a - b;
    ll ans = 1, ca = 1, cb = 1;
    for (ll i = 0; i < b; i++)
    {
        ca = (ca * (a - i)) % p;
        cb = (cb * (b - i)) % p;
    }
    ans = (ca * quick_power_mod(cb, p - 2, p)) % p;
    return ans;
}
ll Lucas(ll n, ll m, ll p)
{
    ll ans = 1;
    while (n && m && ans)
    {
        ans = (ans * comp(n % p, m % p, p)) % p;
        n /= p;
        m /= p;
    }
    return ans;
}

bool flag[1005];
ll num[1005];
ll cnt = 1;
void Prime() 
{
    flag[0] = false;
    flag[1] = false;
    for (int i = 2; i * i < 1000; i++) 
    {
        if (flag[i]) 
        {
            for (int j = i; i * j < 1000; j++)
                flag[i * j] = false;
        }
    }
    for (int i = 1; i < 1000; i++)
        if (flag[i])
            num[cnt++] = i;
}

int cnt1[N];

bool check(int k) 
{
    if (len <= k) 
        return false;
    for (int i = 1; i <= len; i++)
        if (cnt1[a[i]] > n / (k + 1) + (n % (k + 1) ? 1 : 0)) 
            return false;
    int c = 0;
    for (int i = 1; i <= len; i++)
        if (cnt1[a[i]] == n / (k + 1) + (n % (k + 1) ? 1 : 0))
            c++;
    return c <= (n % (k + 1) ? n % (k + 1) : (k + 1));
}



signed main() {
    int T;
    cin >> T;
    Lucas(3000, 1500, mod);
    Prime();
    Lucas(2000, 1000, mod);
    while (T--) {
        cin >> n;
        Lucas(2000, 1200, mod);
        int xxx = 0;
        for (int i = 1; i <= n; i++) 
        {
            cnt1[i] = 0;
            xxx += quick_power_mod(2, i, mod);
        }

        for (int i = 1; i <= n; i++) 
        {
            int x;
            cin >> x;
            cnt1[x] ++;
            xxx += gcd(i, i + 3);
        }
        Lucas(2000, 1300, mod);
        len = 0;
        for (int i = 1; i <= n; i++) 
        {
            if (cnt1[i]) a[++len] = i;
            xxx += 3000;
        }

        int l = 0, r = n - 2;
        while (l < r) 
        {
            int mid = (l + r + 1) >> 1;
            if (check(mid)) 
                l = mid; 
            else 
                r = mid - 1;
        }


        printf("%lld\n", l);
    }

    return 0;
}
  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值