View Code of Problem 2594

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int M = 660000         ;
const ll P = 10000000097ll   ;
const int INF = 0x3f3f3f3f   ;
const int MAX_N = 20         ;
const int MAXSIZE = 101000000;

const int MK = 100005;

const int N = 1005;

int b;

ll n, a[N];

ll cal(ll n) {                          //求出[0,n - 1] 时间内所有理发师理发的总人数
    ll ret = 0;
    for (int i = 0; i < b; ++i) {
        ret += (n - 1 + a[i]) / a[i];   //n - 1 表示(n - 1) 的时间, 表达式表示第i个理发师在前(n-1)时间内理发的人数
    }
    return ret;
}

int main() {
    ofstream fout ("B-large-practice.out");
    ifstream fin ("B-large-practice.in");

    int T, i, j, k;
    cin >> T;
    while (T--) {
        cin >> b >> n;
        for (i = 0; i < b; ++i) {
            cin >> a[i];
        }
        ll l = 0, r = n * MK;
        while (l < r) {
            ll mid = l + r >> 1;
            if (cal(mid) < n) {
                l = mid + 1;
            } else {
                r = mid;
            }
        }
        int last = cal(l - 1), ans = 0;
        for (i = 0; i < b; ++i) {
            if ((l - 1) % a[i] == 0) {
                ++last;
                if (last == n) {
                    ans = i + 1;
                    break;
                }
            }
        }
        static int numCase = 0;
        cout << "Case #" << ++numCase << ": " << ans << endl;
    }
    return 0;
}

Double click to view unformatted code.


Back to problem 2594