Uva 12765 - Factorial Products


Accepted 
 
// Header file begin
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<vector>
#include<cmath>
#include<cctype>
#include<sstream>
#include<set>
#include<list>
#include<stack>
#include<utility>
#include<queue>
#include<algorithm>
#include<cstdlib>
// End
//..........
// Macro
#define sf scanf
#define pf printf
#define lp1(i,n) for(i=0;i<n;i++)
#define lp2(i,n) for(i=1;i<=n;i++)
#define mem(c,v) memset(c,v,sizeof(c))
#define cp(a) cout<<" "<<a<<" "
#define nl puts("")
#define sq(x) ((x)*(x))
#define all(x) x.begin(),x.end()
#define reall(x) x.rbegin(),x.rend()
#define sz size()
#define gc getchar()
#define pb push_back
#define freader freopen("input.txt","r",stdin)
// End.........

// Size
#define mx7 20000100
#define mx6 1500000
#define mx5 100005
#define mx4 1000100
#define inf 1<<30                                           //infinity value
#define eps 1e-9
#define mx (65540)
#define mod 1000000007
#define pi acos(-1.0)

// Macros for Graph

#define white 1
#define gray 2
#define black 3
#define nil 0

using namespace std;
/***************/

// typedef

typedef long long LL;
typedef long L;
typedef unsigned long long ull;
typedef unsigned long ul;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int>vi;
typedef vector<long long> vll;
typedef vector<long>vl;
typedef vector<char>vch;
typedef vector<string>vs;
typedef map<int,int>mpii;
typedef map<int,bool>mpbi;
typedef map<long,bool>mpbl;
typedef map<long long,bool>mpbll;
typedef map<char,int>mpci;
typedef map<char,bool>mpbc;
typedef map<string,int>mpsi;
typedef map<long long,long long>mpll;

// template

template<class T> T gcd(T a, T b ) {return b<=0?a:gcd(b,a%b);}
template<class T> T large(T a, T b ) {return a>b?a:b;}
template<class T> T small(T a, T b ) {return a<b?a:b;}
template<class T> T diffrnce(T a, T b) {return a-b<0?b-a:a-b;}

int prime[(mx>>6)+1],prm[(mx>>1)+9],plen=0,qrt=(int)sqrt(double(mx));
int ek[mx+9],dui[mx+9],eklen=0,duilen=0;
char ekchar[mx+9],duichar[mx+9];

#define setbit(n) (prime[n>>6] |= (1<<((n>>1)&31)))
#define checkbit(n) (prime[n>>6] & (1<<((n>>1)&31)))

void sieve()
{
    int i,j;

    for(i=3;i<=qrt;i+=2)
    {
        if(!checkbit(i))
        {
            for(j=i*i;j<=mx;j+=i+i)
            {
                setbit(j);
            }
        }
    }

    prm[plen++]=2;

    for(i=3;i<=mx;i+=2)
    {
        if(!checkbit(i))
        {
            prm[plen++]=i;
        }
    }

    //lp1(i,100)cout<<prm[i]<<" ";
}

void factox(int num)
{
    for(int i=0;i<plen and sq(prm[i]) <= num ;i++)
    {
        if(!(num%prm[i]))
        {
            while(!(num%prm[i]))
            {
                ek[eklen++]=prm[i];
                num/=prm[i];
                if(num==0 or num==1) break;
            }
        }
    }

    if(num>1)
    {
        ek[eklen++]=num;
    }
}

void factoy(int num)
{
    for(int i=0;i<plen and sq(prm[i])<=num;i++)
    {
        if(!(num%prm[i]))
        {
            while(!(num%prm[i]))
            {
                dui[duilen++]=prm[i];
                num/=prm[i];
                if(num==0 or num==1)break;
            }
        }
    }

    if(num>1)
    {
        dui[duilen++]=num;
    }
}

void fek(int num)
{
    for(int i=2;i<=num;i++)
    {
        //ek[eklen++]=i;
        factox(i);
    }
}

void fdui(int num)
{
    for(int i=2;i<=num;i++)
    {
        //dui[duilen++]=i;
        factoy(i);
    }
}

int main()
{
    sieve();
    int n,m;
    while(~sf("%d %d",&n,&m))
    {
        if(n==0 and m==0) break;

        mem(ek,0);
        mem(dui,0);
        mem(ekchar,0);
        mem(duichar,0);
        eklen=0;duilen=0;
        int ekc=0,duic=0;
        int x;

        while(n--)
        {
            sf("%d",&x);
            if(x==1)continue;
            fek(x);
        }

        while(m--)
        {
            sf("%d",&x);
            if(x==1)continue;
            fdui(x);
        }

        sort(ek,ek+eklen);
        sort(dui,dui+duilen);

        //for(int i=0;i<eklen;i++)cout<<ek[i]<<" "; nl;
        //for(int i=0;i<duilen;i++)cout<<dui[i]<<" "; nl;

        for(int i=0;i<eklen;i++)
        {
            //build_charek(ek[i]);
            ekchar[ekc++]=(ek[i]+'0');
        }

        for(int i=0;i<duilen;i++)
        {
            //build_chardui(dui[i]);
            duichar[duic++]=(dui[i]+'0');
        }

        //for(int i=0;i<ekc;i++)cout<<ekchar[i]<<" "; nl;
        //for(int i=0;i<duic;i++)cout<<duichar[i]<<" "; nl;

        if(!strcmp(ekchar,duichar))
        {
            pf("YES\n");
        }

        else
        {
            pf("NO\n");
        }
    }

    return 0;
}
 

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number