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
Showing posts with label Binary Search. Show all posts
Showing posts with label Binary Search. Show all posts

Friday, June 3, 2016

EIUASSEMBLY-Assembly line

Assembly line

Given below c++ code is for EIUASSEMBLY spoj or Assembly line spoj.

Hint:- Simple Binary Search Implementation.





/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 03 June 2016 (Friday) 10:26
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int , int>
#define ll long long
#define mp make_pair
#define pb push_back
bool check(vector<int> &p, vector<int> &m, ll &total_cost, ll level){
    ll total = 0;
    for(int i = 0 ; i < p.size() ; i++){
        ll diff = level - p[i];
        if(diff > 0)
            total += diff*(ll)m[i];
        if(total > total_cost)
            return false;
    }
    return true;
}
ll b_search(vector<int> &p, vector<int> &m , ll total_cost){
    ll low = 0 , high = (LLONG_MAX/10), mid , ans = -1;
    while(low < high){
        mid = (low + high) >> 1;
        if(check(p, m, total_cost , mid)){
            low = mid+1;
            ans = max(mid , ans);
        } else high = mid;
    }
    return ans;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        ll n , total_cost;
        scanf("%lld%lld", &n , &total_cost);
        vector<int> p(n) , m(n);
        for(int i = 0 ; i < n ; i++){
            scanf("%d%d" , &p[i] , &m[i]);
        }
        printf("%lld\n" , b_search(p , m , total_cost));
    }
    return 0;
}

Saturday, October 24, 2015

BOOKS1-Copying Books

Copying Books

Given below c++ code is for books1 spoj or cpoying books spoj.

Hint:-Simple Binary Search implementation.



/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 24 October 2015 (Saturday) 10:28
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mod 1000000009
bool check(int A , vector<int> &v , ull mid){
    int  n = v.size() , i = 0 , tot = 1;
    ull count = 0;
    while(i < n)
    {
        if((long long)v[i] > mid)
            return false;
        if(count + (long long )v[i] > mid){
            tot++;
            count = 0;
        }
        if(count <= mid){
            count += (long long)v[i];
            i++;
        }
    }
    if(tot <= A)
        return true;
    return false;
}
bool print(int pos , int k ,vector<int> &v , ull mid){
    ull count = 0;
    int pos_till = -1;
    for(int i = pos ; i>= 0 ; i--){
        if(count + v[i] > mid || i == k-2){
            print(i , k-1  , v , mid);
            pos_till = i;
            break;
        }
        count += v[i];
    }
    if(pos_till >= 0)
        printf("/ ");
    for(int i = pos_till +1 ; i<= pos ; i++)
        printf("%d ", v[i]);
}
ull binary_search(vector<int> &v , int A){
    ull low = 1 , high = LLONG_MAX , mid , ans = LLONG_MAX;
    while(low < high){
        mid = (low + high)>>1;
        if(check(A , v , mid)){
            ans = min(ans , mid);
            high = mid;
        } else
            low = mid+1;
    }
    return ans;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int m , k;
        scanf("%d%d",&m,&k);
        vector<int> v(m);
        for(int i = 0 ; i < m ; i ++){
            scanf("%d",&v[i]);
        }
        v.pb(0);
        ull ans = binary_search(v , k);
        print(v.size() - 2 , k , v , ans);
        printf("\n");
    }
    return 0;
}

Sunday, October 18, 2015

PATULJCI-Snow White and the N dwarfs

Snow White and the N dwarfs

Given below code is for patuljci spoj or snow white and the N dwarfs spoj .

Hint:- source( http://hsin.hr/ )

For now, forget about the problem we are solving.

Suppose we had a sequence of integers and an algorithm like this:

while there are different numbers in the sequence
    select any two different numbers from the sequence
        and erase them

For example if the sequence were:
1 2 3 1 2 3 2 3
the algorithm could have done this:
1 2 3 1 2 3 2 3 --> 3 1 2 3 2 3 -> 1 3 2 3 -> 3 3
Note that we could also end up with other sequences if we selected pairs in a different way.

Let candidate be the number that is left in a sequence (3 in the example above).
Let count be the number of numbers left in a sequence (2 in the example above).

The cool thing about the algorithm is that if there is a number that appears more than N/2 times in the sequence it must end up as a candidate no matter the way we select pairs. Intuitively, we don't have enough other numbers to kill all of the candidate numbers. So we can choose our own way to select pairs. Let's do it recursively like this.

1) Split the sequence S in two halves L and R.
2) Run the recursive algorithm on sequence L to get L.candidate and L.count
3) Run the recursive algorithm on sequence R to get R.candidate and R.count
4) Kill the remaining pairs among L.count + R.count numbers that are left.
The step #4 can be done very efficiently like this:

if L.candidate == R.candidate
    S.candidate = L.candidate
    S.count = L.count + R.count
else
    if L.count > R.count
        S.candidate = L.candidate
        S.count = L.count - R.count
    else
        S.candidate = R.candidate
        S.count = R.count - L.count
    end
end

So, we can use this idea to build the interval tree, every node containing info (candidate and count) about the sub-sequence it represents. We can also query the interval tree to get candidate number for any interval [A, B]. Then we can use binary search to count the number of appearances of the candidate number in the interval and determine if the picture is pretty or not.




/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 18 October 2015 (Sunday) 17:10
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mod 1000000009
template<class T>
class segmentTree{
public:
    segmentTree(){
        height = 1;
        left_most = 1<<height;
        right_most = (left_most<<1) - 1; 
        tree = new T[right_most];
    }
    segmentTree(int s){ 
        size=s;
        height = ceil(log2(s));
        left_most = 1<<height;
        right_most = (left_most<<1) - 1;
        tree = new T[right_most+9];
    }
    void init(T * arr){
        build(arr);
    }
    void Update(int pos , T val){
        point_update(1 , left_most , right_most , left_most+pos , val);
    }
    void Update(int l , int r , T val){
        range_update(1 , left_most , right_most , left_most+l , left_most+r , val);
    }
    T Query(int pos){
        return point_query(1 , left_most , right_most , left_most+pos);
    }
    T Query(int l ,int r){
        return range_query(1 , left_most , right_most , left_most+l , left_most+r);
    }
private:
    T *tree;
    int size , left_most , right_most , height;
    void build(T * arr){
        for(int i = 0 ; i < size ; i++)
            tree[left_most+i] = arr[i];
        initalize(1 , left_most , right_most); 
    }
    void initalize(int root , int left_most , int right_most){
        if(left_most == right_most) return;
        int mid = (left_most + right_most)>>1 , l_child = (root<<1)  , r_child = (root<<1)+1;
        tree[root].split(tree[l_child] , tree[r_child]);
        initalize(l_child , left_most , mid);
        initalize(r_child , mid+1 , right_most);
        tree[root].merge(tree[l_child] , tree[r_child]);
    }
    void point_update(int root , int left_most , int right_most , int pos , T val){
        if(left_most == right_most && root == pos) { tree[root].update(val); return ;}
        int mid = (left_most + right_most)>>1 , l_child = root<<1 , r_child = (root<<1)+1;
        tree[root].split(tree[l_child] , tree[r_child]);
        if(pos <= mid) point_update(l_child , left_most , mid , pos , val);
        else point_update(r_child , mid+1 , right_most , pos , val);
        tree[root].merge(tree[l_child] , tree[r_child]);
    }
    void range_update(int root , int left_most , int right_most , int l , int r , T val){
        if(l <= left_most && r >= right_most){ tree[root].update(val);return;}
        int mid = (left_most + right_most)>>1 , l_child = root<<1 , r_child = (root<<1)+1;
        tree[root].split(tree[l_child] , tree[r_child]);
        if(l <= mid) range_update(l_child , left_most , mid, l , r , val);
        if(r > mid) range_update(r_child , mid+1 , right_most , l , r , val);
        tree[root].merge(tree[l_child] , tree[r_child]);
    }
    T range_query(int root , int left_most ,int right_most ,int l , int r){
        if( l <= left_most && r >= right_most )
            return tree[root];
        int mid = (left_most + right_most)>>1 , l_child = root<<1 , r_child = (root<<1)+1;
        tree[root].split(tree[l_child] , tree[r_child]);
        T l_node  , r_node , temp;
        if(l <= mid) l_node = range_query(l_child , left_most , mid , l , r );
        if(r > mid) r_node = range_query(r_child , mid+1 , right_most , l , r );
        tree[root].merge(tree[l_child] , tree[r_child]);
        temp.merge(l_node , r_node);
        return temp;
    }
    T point_query(int root , int left_most , int right_most , int pos){
        if(left_most == right_most && root == pos) return tree[root];
        int mid = (left_most + right_most)>>1 , l_child = root<<1 , r_child = (root<<1)+1;
        T temp;
        tree[root].split(tree[l_child] , tree[r_child]);
        if(pos <= mid) temp = point_query(l_child , left_most , mid , pos);
        else temp = point_query(r_child , mid+1 , right_most , pos);
        tree[root].merge(tree[l_child] , tree[r_child]);
        return temp;
    }
};
class node{
public:
    int count , number;
    void merge(node &l , node &r){
        if(l.number == r.number){
            count = l.count + r.count;
            number = l.number;
        } else {
            if( l.count > r.count ){
                count = l.count - r.count;
                number = l.number;
            } else{
                count = r.count - l.count;
                number = r.number;
            }
        }
    }
    void split(node &a , node &b){
    }
    void update(node &a){
    }
    node(){
        count = 0  , number = 0;
    }
    node(int num){
        count = 1;
        number = num;
    }
};
node arr[300009];
vector<int> Index[100009];
int main(){
    int n , c;
    scanf("%d%d",&n , &c);
    int temp;
    for(int i = 0; i < n ; i++){
        scanf("%d",&temp);
        Index[temp].pb(i);
        arr[i] = node(temp);
    }
    segmentTree<node> s(n);
    s.init(arr);
    int q , l , r;
    scanf("%d",&q);
    while(q--){
        scanf("%d%d",&l,&r);
        l-- , r--;
        int num = s.Query(l , r).number;
        int count = upper_bound(Index[num].begin() , Index[num].end() , r) - lower_bound(Index[num].begin() , Index[num].end() , l);
        int req = r - l +1;
        if(count*2 > req){
            printf("yes %d\n", num);
        } else
            printf("no\n");
    }
    return 0;
} 

Wednesday, August 12, 2015

EKO-Eko

Eko

Given below code is for EKO spoj or Eko spoj.
Simple Binary Search implementation.



/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 12 August 2015 (Wednesday) 02:14
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mod 1000000009
ll woodCut(vector<ll> &v , ll h){
    ll sum = 0;
    for(int i = 0 ; i < v.size() ; i++){
        if(v[i] > h)
            sum += v[i]-h;
    }
    return sum;
}
ll b_search(vector<ll> &v , ll required){
    ll low = 0 , mid , high = LLONG_MAX/10;
    ll ans = LLONG_MIN;
    while(low < high){
        mid = (low + high)>>1;
        ll wood = woodCut(v , mid);
        if(wood >= required){
            low = mid+1;
            ans = max(ans , mid);
        }
        else
            high = mid;
    }
    return ans;
}
int main(){
    
    ll n , m;
    scanf("%lld%lld" , &n , &m);
    vector<ll> v(n);
    for(int i = 0 ; i < n ; i++){
        scanf("%lld",&v[i]);
    }
    printf("%lld\n",b_search(v , m));
    return 0;
}

KOPC12A-K12 - Building Construction

K12 - Building Construction

Given Below c++ code is for kopc12a spoj or k12-building construction spoj.
Simple Binary Search question.



/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 12 August 2015 (Wednesday) 01:47
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mod 1000000009
ll check(vector<int> &v ,vector<int> &c, int h){
    ll sum = 0;
    for(int i = 0 ; i < v.size() ; i++){
        ll diff = abs(v[i] - h);
        sum += diff*c[i];
    }
    return sum;
}
ll b_search(vector<int> &v , vector<int>&c){
    ll low = 0 , mid , high = INT_MAX;
    ll p , n , m , ans = LLONG_MAX;
    while(low<high){
        mid = (low+high)>>1;
        mid > 0 ? p = check(v , c , mid-1): INT_MAX;
        m = check(v , c , mid);
        n = check(v , c , mid+1);
        if(ans == m)
            break;
        ans = min(ans , m);
        if(p <= m)
            high = mid;
        else if(n <= m)
            low = mid+1;
    }
    return ans;
}
int main(){
    
    int t;
    scanf("%d" , &t);
    while(t--){
        int n , temp;
        scanf("%d",&n);
        vector<int> v(n) , c(n);
        for(int i = 0 ; i < n ; i++){
            scanf("%d",&temp);
            v[i] = temp;
        }
        for(int i = 0 ; i < n ; i++){
            scanf("%d",&temp);
            c[i] = temp;
        }
        printf("%lld\n", b_search(v , c));
    }
    
    return 0;
}

Tuesday, December 16, 2014

Tuesday, October 21, 2014

SVADA - Svada

Svada

Given below code is for svada spoj.

Hint : -> Binary search :

Here actually what I am doing is partitioning time in two part and checking
1->how many coconut can be plucked by first group of monkey in first part (let say X the number of coconut)
2-> how many coconut can be braked by second group of monkey in second part(say Y - the number of coconut)

let say for any partition of time if  X <= Y then that partition is possible .
And among such possible partition you have to check for partition which have least Y (according to question)

For checking we have applied binary search on time .



#include <bits/stdc++.h>
using namespace std;
#define LL long long 
LL solve(LL a,LL b,LL t)
{
    LL res = (t - a) < 0 ? 0 : (t-a)/b + 1;
    return res;
}
LL calculate(LL a[] , LL b[] , LL t , int sz)
{
    int total = 0;
    for(int i = 0 ; i < sz ; i++)
        total += solve(a[i] , b[i] , t);
    return  total;
}
LL b_search(LL a[] , LL b[] , LL c[] , LL d[] , LL t ,int sa, int sc)
{
    LL low = 1 , high = t , mid , lhalf , rhalf , ma = -1;
    while(low < high)
    {
        mid = (low + high)>>1;
        lhalf = calculate(a,b,mid,sa);
        rhalf = calculate(c,d,t - mid , sc);
        if(lhalf <= rhalf){
            low = mid + 1;
            ma = max(mid,ma);
        }
        else if(lhalf > rhalf)
            high = mid;
    }
    return ma;
}
int main()
{
    LL t;
    scanf("%lld",&t);
    int sa , sb;
    scanf("%d",&sa);
    LL a[100], b[100] , c[100] , d[100];
    for(int i = 0 ; i < sa ; i++)
        scanf("%lld%lld",&a[i] , &b[i]);
    scanf("%d",&sb);
    for(int i = 0 ;i<sb ; i++)
        scanf("%lld%lld",&c[i] ,&d[i]);
    printf("%lld\n", b_search(a,b,c,d,t,sa,sb));
    return  0;
}

Friday, October 17, 2014

PRATA - Roti Prata

Roti Prata

Given below code is for prata spoj or roti prata spoj.

Hint :-> Use Binary Search 

This problem can also be solved by BFS also but i don't know how to solve it using bfs/dfs if any one know it then please comment of mail me your approach @ raj.nishant360@gmail.com.



#include <bits/stdc++.h>
using namespace std;
#define ULL unsigned long long 
int n;
ULL get_num(int a,ULL sum)
{
    ULL d = (ULL)sqrt(a*a + 8*a*sum);
    d-=a;
    return d / (2*a);
}
int check(int arr[],ULL chef,ULL tim)
{
    ULL count = 0;
    for(ULL i =0 ;i<chef;i++)
    {
        count += get_num(arr[i],tim);
        if(count >= n)
            return 1;
    }
    return 0;
}
ULL binary_search(int arr[],ULL chef,ULL max)
{
    ULL low = 1,mid,high = max;
    while(low<high)
    {
        mid = (low+high)>>1;
        if(check(arr,chef,mid))
            high = mid;
        else
            low = mid + 1;
    }
    return low;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int chef;
        scanf("%d%d",&n,&chef);
        int arr[chef+9],ma = -1;
        for(int i = 0;i<chef ; i++){
            scanf("%d",&arr[i]);
        }
        sort(arr,arr+chef);
        ULL res = binary_search(arr,chef,(ULL)10*n*(n+1)/2);
        printf("%lld\n",res);
    }
    return 0;
}

Sunday, July 27, 2014

PIE-pie

Below given code is for pie spoj.
Hint : - Think Binary search,


#include <bits/stdc++.h>
using namespace std;
int n,f;
long double pi=3.14159265358979323846264338327950;
int func(long double num,long double array[])
{
    int fr=0;
    if (num==0)
        return 0;
    for (int i=0; i<n; i++)
        fr+=(int)(array[i]/num);
    if (fr>=f)
        return 1;
    else
        return 0;
}
long double bs(long double array[])
{
    long double ini=0,last=array[n-1],max=0.0;
    while (last - ini >= 1e-6)
    {
        //printf("%.2Lf %.2Lf\n",ini,last);
        long double mid=(ini+last)/2;
        if (func(mid,array)==1)
        {
            /*if (max<mid)
                max=mid;*/
            ini=mid;
        }
        else
            last=mid;
    }
    return ini;
}
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d %d",&n,&f);
        f++;
        int array1[n];
        for (int i=0; i<n; i++)
            scanf("%d",&array1[i]);
        long double array[n];
        sort(array1,array1+n);
        for (int i=0; i<n; i++)
            array[i]=array1[i]*array1[i]*pi;
        /*for (int i=0; i<n; i++)
            printf("%Lf ",array[i]);
        cout<<endl;*/
        long double k=bs(array);
        printf("%.4Lf\n",k);
    }
    return 0;
}

AGGRCOW-Aggressive cows

Below given code is for AGGRCOW spoj Aggressive cows spoj.
Hint:- Think Binary search,

#include <bits/stdc++.h>
using namespace std;
int n,c;
int func(int num,int array[])
{
    int cows=1,pos=array[0];
    for (int i=1; i<n; i++)
    {
        if (array[i]-pos>=num)
        {
            pos=array[i];
            cows++;
            if (cows==c)
                return 1;
        }
    }
    return 0;
}
int bs(int array[])
{
    int ini=0,last=array[n-1],max=-1;
    while (last>ini)
    {
        //cout<<last<<" "<<ini<<endl;
        int mid=(ini+last)/2;
        if (func(mid,array)==1)
        {
            //cout<<mid<<endl;
            if (mid>max)
                max=mid;
            ini=mid+1;
        }
        else
            last=mid;
    }
    return max;
}
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d %d",&n,&c);
        int array[n];
        for (int i=0; i<n; i++)
            scanf("%d",&array[i]);
        sort(array,array+n);
        //cout<<" dfsa \n";
        int k=bs(array);
        printf("%d\n",k);
    }
    return 0;
}

Monday, May 26, 2014

NBIN-New Binary

Below given code is for nbin spoj or new binary spoj.

NBIN spoj New Binary spoj Here in the first we have to formulate the sequence as we can know how many bits can accommodate given “n”;
Now let formulate the table:-
Number of Bits
Binary number at given Nth position
Given N
1
1
1
2
10
2
3
100
3
3
101
4
4
1000
5
4
1001
6
4
1010
7
5
10000
8
5
10001
9
5
10010
10
5
10100
11
5
10101
12

From the above table we can see that five digit binary number can accommodate at most 5 values .
Now by carefully seeing this we can see that if remove the two Most significant Bit from five digit binary number we can see that rest binary number are the number less than three digit binary as see below


10
000
10
001
10
010
10
100
10
101
As we cliff the Left part of table .By the rest we can formulate the DP as:
Position of the array represent the number of Bits and content of array represent the maximum “N” that or less can be represented using that or less number of Bit :
PRE_COMPUTE[i] = PRE_COMPUTE[i-1] + PRE_COMPUTE[i-2] + 1;
Now for getting result take the string of MAX = 72(because (N = 1015  ) Nth number can be accommodated in at max 72 Bit you will get that after calculating) initially containing all ZEROES now for the given “N” find the minimum POSITION in array  which can accommodate Nth number and after that make
N = N - PRE_COMPUTE[POSITION -1] - 1 .And repeat till N>0;(you will get this if you use pen and paper and write few steps)

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define LEN 72
LL pre_com[100];
void pre()
{
    pre_com[1] = 1;
    pre_com[2] = 2;
    pre_com[3] = 4;
    pre_com[4] = 7;
    int i=4;
    while(pre_com[i] <=1000000000000000LL)
    {
        i++;
        p[i] = p[i-2]+1;
        pre_com[i] =pre_com[i-1] + pre_com[i-2]+1;
    }
}
int find(LL n)
{
    int low=0,high=LEN,mid;
    mid = (low + high)>>1;
    while(true)
    {
        mid = (low + high)>>1;
        if(pre_com[mid] < n)
        {
            if(pre_com[mid + 1] >n)
                return mid;
            else
                low = mid;
        }
        else
        {
            if(pre_com[mid - 1] < n)
                return mid - 1;
            else
                high = mid;
        }
    }
}
void print(LL n)
{
    bool a[80]={false};
    int pos  = find(n);
    int max = pos + 1;
    a[pos+1]=true;
    LL diff = n - pre_com[pos] - 1;
    while(diff>0)
    {
        pos = find(diff);
        a[pos+1] = true;
        diff = diff - pre_com[pos] - 1;
    }
    for(int i=max ;i>=1;i--)
        if(a[i])
            printf("1");
        else
            printf("0");
}
int main()
{
    pre();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        LL n;
        scanf("%lld",&n);
        print(n);
        printf("\n");
    }
    return 0;
}

Thursday, May 22, 2014

WPC5I-LCM

LCM

given below c code for wpc5i spoj or lcm spoj.
If you have any problem you can ask me @ raj.nishant360@gmail.com
#include <bits/stdc++.h>
using namespace std;
int main()
{
 int t;
 scanf("%d",&t);
 while(t--)
 {
  int n,m;
  scanf("%d%d",&n,&m);
  map<int ,int>mp1,mp2,result;
  map<int ,int>::iterator t;
  for(int i=2;i*i<=n;i++)
  {
       while(n%i==0)
       {
           mp1[i]+=1;
           n/=i;
        }
  }
  if(n>1)
     mp1[n]+=1;
  for(int i=2;i*i<=m;i++)
  {
        while(m%i==0)
        {
            mp2[i]+=1;
            m/=i;
        }
  }
  if(m>1)
     mp2[m]+=1;
  long long k=1;
  for(t=mp1.begin();t!=mp1.end();t++)
  {
       if(t->second > mp2[t->first])
           result[t->first]=t->second;
  } 
  for(t=mp2.begin();t!=mp2.end();t++)
  {
       if(t->second > mp1[t->first] && result[t->first] < t->second)
            result[t->first]=t->second;
  }
  for(t=result.begin();t!=result.end();t++)
       for(int i=0;i<t->second;i++)
            k*=(long long)(t->first);
  printf("%lld\n",k);
 }
 return 0;
}