Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com
And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Tuesday, December 16, 2014

CURDPROD-CURD PRODUCERS

CURD PRODUCERS

Given below c++ code is for curdprod spoj or curd producers spoj.

Hint :- simple binary search problem .




#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL check(LL t , int n , int arr[]){
    LL total = 0;
    for(int i = 0 ; i < n ; i++)
        total += t / arr[i];
    return total;
}
LL binary_search(int arr[] , int n , LL product , LL ma){
    LL mid , low = 1 , high = ma;
    while(low < high){
        mid = (low + high)>>1;
        LL temp = check(mid , n , arr);
        if(temp < product)
            low = mid + 1;
        else if(temp >= product)
            high = mid;
    }
    return high;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n , T;
        scanf("%d%d",&n,&T);
        int arr[n+9];
        LL ma = -1;
        for(int i = 0 ; i < n ; i++){
            scanf("%d",&arr[i]);
            ma = max(ma , (LL)arr[i]);
        }
        ma = ma * T;
        printf("%lld\n",binary_search(arr, n , T , ma));
    }
    return 0;
}