View Code of Problem 2594

#include<stdio.h>
#define ll long long int 

const int MK = 100005;
const int N = 1005; 
int b;
ll n, a[N];
 
ll cal(ll n) {                    
    ll ret = 0;
    for (int i = 0; i < b; ++i) {
        ret += (n - 1 + a[i]) / a[i];   
    }
    return ret;
}
 
int main() {
    int T, i, j, k;
    scanf("%d",&T);
    while (T--) {
       scanf("%d%d",&b,&n);
        for (i = 0; i < b; ++i) {
            scanf("%d",&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;
       printf("Case #%d: %d\n",++numCase,ans );

    }
    return 0;
}
/*
Main.c:7:7: error: variably modified ‘a’ at file scope
 ll n, a[N];
       ^
Main.c: In function ‘main’:
Main.c:21:8: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘long long int *’ [-Wformat=]
        scanf("%d%d",&b,&n);
        ^
Main.c:23:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘long long int *’ [-Wformat=]
             scanf("%d",&a[i]);
             ^
Main.c:27:24: warning: suggest parentheses around ‘+’ inside ‘>>’ [-Wparentheses]
             ll mid = l + r >> 1;
                        ^
Main.c:18:18: warning: unused variable ‘k’ [-Wunused-variable]
     int T, i, j, k;
                  ^
Main.c:18:15: warning: unused variable ‘j’ [-Wunused-variable]
     int T, i, j, k;
               ^
*/

Double click to view unformatted code.


Back to problem 2594