(LightOJ) 1218 – Multiple Free Subset

0

Step 1. Sort the array.
Step 2. Remove duplicates.
Step 3. Make a bipartite graph. There should be a connection from i to j iff i divides j.
Step 4. Start from the smallest number, and follow the next steps repeatedly.

– Remove all the multiples of this number from the graph. Remove from both sides of bipartite graph.
– Now check, if we can create exactly same size of maximum independent set as before.
– If possible, parmanently delete those multiples and push the current number into result array. If not possible, restore the previous graph.


/***********Template Starts Here***********/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include
<map>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <functional>
#include <string>
#include <iostream>
#include <cctype>
#include <set>
#include <climits>
#include <iomanip>
#include <cassert>
//#include <unordered_map>

#define pb push_back
#define nl puts ("")
#define sp printf ( " " )
#define phl printf ( "hello\n" )
#define ff first
#define ss second
#define POPCOUNT __builtin_popcountll
#define RIGHTMOST __builtin_ctzll
#define LEFTMOST(x) (63-__builtin_clzll((x)))
#define MP make_pair
#define FOR(i,x,y) for(int i = (x) ; i <= (y) ; ++i) #define ROF(i,x,y) for(int i = (y) ; i >= (x) ; --i)
#define CLR(x,y) memset(x,y,sizeof(x))
#define UNIQUE(V) (V).erase(unique((V).begin(),(V).end()),(V).end())
#define MIN(a,b) ((a)<(b)?(a):(b)) #define MAX(a,b) ((a)>(b)?(a):(b))
#define NUMDIGIT(x,y) (((int)(log10((x))/log10((y))))+1)
#define SQ(x) ((x)*(x))
#define ABS(x) ((x)<0?-(x):(x))
#define FABS(x) ((x)+eps<0?-(x):(x)) #define ALL(x) (x).begin(),(x).end() #define LCM(x,y) (((x)/gcd((x),(y)))*(y)) #define SZ(x) ((int)(x).size()) #define NORM(x) if(x>=mod)x-=mod;

using namespace std;

typedef long long vlong;
typedef unsigned long long uvlong;
typedef pair < int, int > pii;
typedef pair < vlong, vlong > pll;
typedef vector<pii> vii;
typedef vector<int> vi;

const vlong inf = 2147383647;
const double pi = 2 * acos ( 0.0 );
const double eps = 1e-9;

#ifdef forthright48
     #include <ctime>
     clock_t tStart = clock();
     #define debug(args...) {dbg,args; cerr<<endl;}
     #define timeStamp printf("Execution Time: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC)
#else
    #define debug(args...)  // Just strip off all debug tokens
    #define timeStamp
#endif

struct debugger{
    template<typename T> debugger& operator , (const T& v){
        cerr<<v<<" ";
        return *this;
    }
}dbg;

//int knightDir[8][2] = { {-2,1},{-1,2},{1,2},{2,1},{2,-1},{-1,-2},{1,-2},{-2,-1} };
//int dir4[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

inline vlong gcd ( vlong a, vlong b ) {
    a = ABS ( a ); b = ABS ( b );
    while ( b ) { a = a % b; swap ( a, b ); } return a;
}

vlong ext_gcd ( vlong A, vlong B, vlong *X, vlong *Y ){
    vlong x2, y2, x1, y1, x, y, r2, r1, q, r;
    x2 = 1; y2 = 0;
    x1 = 0; y1 = 1;
    for (r2 = A, r1 = B; r1 != 0; r2 = r1, r1 = r, x2 = x1, y2 = y1, x1 = x, y1 = y ) {
        q = r2 / r1;
        r = r2 % r1;
        x = x2 - (q * x1);
        y = y2 - (q * y1);
    }
    *X = x2; *Y = y2;
    return r2;
}

inline vlong modInv ( vlong a, vlong m ) {
    vlong x, y;
    ext_gcd( a, m, &x, &y );
    if ( x < 0 ) x += m; //modInv is never negative     return x; } inline vlong power ( vlong a, vlong p ) {     vlong res = 1, x = a;     while ( p ) {         if ( p & 1 ) res = ( res * x );         x = ( x * x ); p >>= 1;
    }
    return res;
}

inline vlong bigmod ( vlong a, vlong p, vlong m ) {
    vlong res = 1 % m, x = a % m;
    while ( p ) {
        if ( p & 1 ) res = ( res * x ) % m;
        x = ( x * x ) % m; p >>= 1;
    }
    return res;
}

/***********Template Ends Here***********/

/*********** Hasib Templates Starts Here**********/

#include<bits/stdc++.h>
using namespace std;

#define loop(i, n) for(int i=0; i<(n); i++)
#define sf scanf
#define pf printf
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) (v).begin(), (v).end()
#define PI acos(-1.0)
#define mem(ara, val) memset(ara, val, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)
#define vdump(x) cerr<<#x<<" = "<<x<<endl;
#define dump(args...) cerr,args; cerr<<endl

template<typename T>
ostream& operator<<(ostream& out, vector<T> v)
{
    out<<"[ ";

    loop(i, SZ(v))
    {
        if(i) out<<", ";
        out<<v[i];
    }

    out<<" ]";
    return out;
}

template<typename T1, typename T2>
ostream& operator<<(ostream &out, pair<T1, T2> p)
{
    out<<"( "<<p.fr<<", "<<p.sc<<")";
    return out;
}

template<typename T>
ostream& operator,(ostream &out, T x)
{
    out<<x<<" ";
    return out;
}

/**********Hasib Templates Ends Here**************/

/**
 *  WARTNING for me:
 *     Never use    FOR
 *     Never use    pii, pll, vi, vi
 *     Never use    ff, ss, phl, sp, nl
 */

int N;

vector<int>v;
#define NODE 107

int mark[NODE];

struct KUHN{
        int left[NODE], right[NODE], vis[NODE], cc;
        vector<int>adj[NODE];

        KUHN() : cc(1) {}

        void clear(int n)
        {
                FOR(i, 0, n) adj[i].clear();
        }

        bool tryK(int v)
        {
                if (vis[v] == cc) return false;
                vis[v] = cc;

                loop(i, SZ(adj[v]))
                {
                        int t = adj[v][i];

                        if(mark[t]) continue;

                        if(right[t] == -1)
                        {
                                right[t] = v; left[v] = t;
                                return true;
                        }
                }

                loop(i, SZ(adj[v]))
                {
                        int t = adj[v][i];

                        if(mark[t]) continue;

                        if(tryK(right[t]))
                        {
                                right[t] = v; left[v] = t;
                                return true;
                        }
                }

                return false;

        }

        int match(int n)
        {
                int res = 0;
                bool done;

                CLR(left, -1); CLR(right, -1);

                do{
                        done = true;
                        cc++;
                        FOR(i, 0, n)
                        {
                                if(mark[i]) continue;

                                if(left[i] == -1 && tryK(i))
                                {
                                        done = false;
                                }
                        }
                }while(!done);

                FOR(i, 0, n)
                {
                        if(mark[i]) continue;

                        res += (left[i] != -1);
                }
                return res;
        }
}kuhn;

void solve()
{
        sort(all(v));
        UNIQUE(v);

        N = SZ(v);

        kuhn.clear(N + 2);

        loop(i, N)
        {
                for(int j=i+1; j<N; j++)
                {
                        if( (v[j] % v[i]) == 0 )
                        {
                                //vdump(MP(i,j));
                                kuhn.adj[i].pb(j);
                        }
                }
        }

        mem(mark, 0);

        int M = N - kuhn.match(N+2);

        vector<int>result;

        int m;
        int prevMarked = 0;

        loop(i, N)
        {
                if(mark[i]) continue;

                m = 0;

                for(int j=i+1; j<N; j++)
                {
                        if( (mark[j] == 0) && (v[j] % v[i] == 0) )
                        {
                                mark[j] = 2;
                                m++;
                        }
                }

                int tmp = (N - m - prevMarked - kuhn.match(N+2));

                if(tmp == M )
                {
                        result.pb(i);
                        prevMarked += m;
                        m = 1;
                }
                else
                {
                        m = 0;
                }

                loop(i, N)
                {
                        if(mark[i] == 2)
                        {
                                mark[i] = m;
                        }
                }
        }

        loop(i, M)
        {
                pf(" %d", v[result[i]]);
        }

        pf("\n");
}

int main ()
{
    #ifdef hasibpc
        read("input.txt");
        //write("output.txt");
    #endif // hasibpc

    int kases, kaseno = 0;

    sf("%d", &kases);

    while(kases--)
    {
            sf("%d", &N);

            v.resize(N);

            loop(i, N)
            {
                    sf("%d", &v[i]);
            }

            pf("Case %d:", ++kaseno);

            solve();
    }

    return 0;
}

(LightOj) 1205 – Palindromic Numbers

0

/****************************************************************
   ▄█    █▄       ▄████████    ▄████████  ▄█  ▀█████████▄
  ███    ███     ███    ███   ███    ███ ███    ███    ███
  ███    ███     ███    ███   ███    █▀  ███   ███    ███
 ▄███▄▄▄▄███▄▄   ███    ███   ███        ███  ▄███▄▄▄██▀
▀▀███▀▀▀▀███▀  ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄
  ███    ███     ███    ███          ███ ███    ███    ██▄
  ███    ███     ███    ███    ▄█    ███ ███    ███    ███
  ███    █▀      ███    █▀   ▄████████▀  █▀   ▄█████████▀
****************************************************************/



#include<bits/stdc++.h>


#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) v.begin(), v.end()
#define PI acos(-1.0)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define SZ(a) int(a.size())
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)

#define take(args...) asdf,args
#define dump(x) cerr<<#x<<" = "<<x<<endl
#define debug(args...) cerr,args; cerr<<endl;
using namespace std;


template<typename T>
ostream& operator<<(ostream& output, vector<T>&v)
{
    output<<"[ ";
    if(SZ(v))
    {
        output<<v[0];
    }
    FOR(i, 1, SZ(v))
    {
        output<<", "<<v[i];
    }
    output<<" ]";
    return output;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& output, pair<T1, T2>&p)
{
    output<<"( "<<p.fr<<", "<<p.sc<<" )";
    return output;
}




template<typename T>
ostream& operator,(ostream& output, T x)
{
    output<<x<<" ";
    return output;
}



struct ASDF{
    ASDF& operator,(int &a) {
        sf("%d", &a);
        return *this;
    }
    ASDF& operator,(long int &a){
        sf("%ld", &a);
        return *this;
    }
    ASDF& operator,(long long int &a){
        sf("%lld", &a);
        return *this;
    }
    ASDF& operator,(char &c){
        sf("%c", &c);
        return *this;
    }
    ASDF& operator,(double &d){
        sf("%lf", &d);
        return *this;
    }

    template<typename T>
    ASDF& operator,(T &a){
        cin>>a;
        return *this;
    }
}asdf;



//Header ends here



ll countPalindromeOfSize(int len)
{

    int halfLen = len/2;
    if((len%2) == 1)
    {
        halfLen++;
    }

    ll ret = 1;

    for(int i=0; i<halfLen; i++)
    {
        ret *= (ll)10;
    }
    //dump(len);
    //dump(ret);
    return ret;
}




ll countPalindromeTillThisPosition(string str)
{

    loop(i, SZ(str))
    {
        str[i] = str[i] - '0';
    }


    ll ret = 0;


    int len = SZ(str);

    int halfLen = len/2;

    if((len%2) == 1)
    {
        halfLen++;
    }

    for(int i=1; i<len; i++)
    {
        ret += (countPalindromeOfSize(i) / (ll)10)*(ll)9;
        //cerr<<"   ret = "<<ret<<endl;
    }

    //dump(ret);


    loop(i, halfLen)
    {
        if(i == 0)
        {
            ret += (str[i] - 1) * countPalindromeOfSize(len - 2*(i+1));   // -1 for ! zero
        }
        else
        {
            ret += (str[i]) * countPalindromeOfSize(len - 2*(i+1));
        }
    }


    bool flag = true;

    //
    {
        int i, j;
        i = halfLen - 1;
        j = i;

        if((len%2) == 0)
        {
            j++;
        }

        while(i>=0)
        {
            if(str[j] < str[i])
            {
                flag = false;
                break;
            }
            else if(str[j] > str[i])
            {
                break;
            }

            i--;
            j++;
        }
    }

    if(flag) ret++;

    return ret;


}


string toStr(ll num)
{
    if(num == 0)
    {
        return "0";
    }

    string str;

    while(num != 0)
    {
       str.pb( num%10 + '0');
       num /= 10;
    }

    reverse(all(str));

    return str;
}







void init()
{
    //cout<<countPalindromeOfSize(-3);
    //cout<<countPalindromeTillThisPosition("");
    //cout<<toStr(104);

    //read("input");
}


int main()
{
    init();

    int kases, kaseno = 0;

    ll a, b;

    string aa, bb;

    cin>>kases;

    while(kases--)
    {
        cin>>a>>b;

        if(a > b)
        {
            swap(a, b);
        }

        ll res = 0;

        if(a == 0)
        {
            res++;
        }
        else
        {
            a--;
        }

        aa = toStr(a);
        bb = toStr(b);


        res += countPalindromeTillThisPosition(bb) - countPalindromeTillThisPosition(aa);


        pf("Case %d: %lld\n", ++kaseno, res);
    }



    return 0;
}


(LightOj) 1328 – A Gift from the Setter

0

/****************************************************************
   ▄█    █▄       ▄████████    ▄████████  ▄█  ▀█████████▄
  ███    ███     ███    ███   ███    ███ ███    ███    ███
  ███    ███     ███    ███   ███    █▀  ███   ███    ███
 ▄███▄▄▄▄███▄▄   ███    ███   ███        ███  ▄███▄▄▄██▀
▀▀███▀▀▀▀███▀  ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄
  ███    ███     ███    ███          ███ ███    ███    ██▄
  ███    ███     ███    ███    ▄█    ███ ███    ███    ███
  ███    █▀      ███    █▀   ▄████████▀  █▀   ▄█████████▀
****************************************************************/



#include<bits/stdc++.h>


#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) v.begin(), v.end()
#define PI acos(-1.0)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define SZ(a) int(a.size())
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)

#define take(args...) asdf,args
#define dump(x) cerr<<#x<<" = "<<x<<endl
#define debug(args...) cerr,args; cerr<<endl;
using namespace std;


template<typename T>
ostream& operator<<(ostream& output, vector<T>&v)
{
    output<<"[ ";
    if(SZ(v))
    {
        output<<v[0];
    }
    FOR(i, 1, SZ(v))
    {
        output<<", "<<v[i];
    }
    output<<" ]";
    return output;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& output, pair<T1, T2>&p)
{
    output<<"( "<<p.fr<<", "<<p.sc<<" )";
    return output;
}




template<typename T>
ostream& operator,(ostream& output, T x)
{
    output<<x<<" ";
    return output;
}



struct ASDF{
    ASDF& operator,(int &a) {
        sf("%d", &a);
        return *this;
    }
    ASDF& operator,(long int &a){
        sf("%ld", &a);
        return *this;
    }
    ASDF& operator,(long long int &a){
        sf("%lld", &a);
        return *this;
    }
    ASDF& operator,(char &c){
        sf("%c", &c);
        return *this;
    }
    ASDF& operator,(double &d){
        sf("%lf", &d);
        return *this;
    }

    template<typename T>
    ASDF& operator,(T &a){
        cin>>a;
        return *this;
    }
}asdf;



//Header ends here

#define MAXX 100007

#define MOD 1000007

ll a[MAXX];

int K, C, N;

ll v[MAXX];


ll solve()
{
    sort(a, a + N);

    reverse(a, a + N);

    ll ret = 0;

    v[0] = 0;

    FOR(i, 1, N)
    {
        v[i] = v[i-1] + (a[i-1] - a[i]) * (ll)i;
        ret += v[i];
    }

    return ret;

}




void init()
{

}


int main()
{
    init();

    int kases, kaseno = 0;

    take(kases);

    while(kases--)
    {
        take(K, C, N, a[0]);

        FOR(i, 1, N)
        {
            a[i] = (a[i-1]*K + C) % MOD;
        }


        pf("Case %d: %lld\n", ++kaseno, solve());




    }





    return 0;
}

(LightOj) 1374 – Confusion in the Problemset

0

/****************************************************************
   ▄█    █▄       ▄████████    ▄████████  ▄█  ▀█████████▄
  ███    ███     ███    ███   ███    ███ ███    ███    ███
  ███    ███     ███    ███   ███    █▀  ███   ███    ███
 ▄███▄▄▄▄███▄▄   ███    ███   ███        ███  ▄███▄▄▄██▀
▀▀███▀▀▀▀███▀  ▀███████████ ▀███████████ ███ ▀▀███▀▀▀██▄
  ███    ███     ███    ███          ███ ███    ███    ██▄
  ███    ███     ███    ███    ▄█    ███ ███    ███    ███
  ███    █▀      ███    █▀   ▄████████▀  █▀   ▄█████████▀
****************************************************************/



#include<bits/stdc++.h>


#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) FOR(i, 0, n)
#define sf scanf
#define pf printf
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define ll long long
#define dd double
#define all(v) v.begin(), v.end()
#define PI acos(-1.0)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define paii pair<int, int>
#define pall pair<ll, ll>
#define SZ(a) int(a.size())
#define read(nm) freopen(nm, "r", stdin)
#define write(nm) freopen(nm, "w", stdout)

#define take(args...) asdf,args
#define dump(x) cerr<<#x<<" = "<<x<<endl
#define debug(args...) cerr,args; cerr<<endl;
using namespace std;


template<typename T>
ostream& operator<<(ostream& output, vector<T>&v)
{
    output<<"[ ";
    if(SZ(v))
    {
        output<<v[0];
    }
    FOR(i, 1, SZ(v))
    {
        output<<", "<<v[i];
    }
    output<<" ]";
    return output;
}

template<typename T1, typename T2>
ostream& operator<<(ostream& output, pair<T1, T2>&p)
{
    output<<"( "<<p.fr<<", "<<p.sc<<" )";
    return output;
}




template<typename T>
ostream& operator,(ostream& output, T x)
{
    output<<x<<" ";
    return output;
}



struct ASDF{
    ASDF& operator,(int &a) {
        sf("%d", &a);
        return *this;
    }
    ASDF& operator,(long int &a){
        sf("%ld", &a);
        return *this;
    }
    ASDF& operator,(long long int &a){
        sf("%lld", &a);
        return *this;
    }
    ASDF& operator,(char &c){
        sf("%c", &c);
        return *this;
    }
    ASDF& operator,(double &d){
        sf("%lf", &d);
        return *this;
    }

    template<typename T>
    ASDF& operator,(T &a){
        cin>>a;
        return *this;
    }
}asdf;



//Header ends here

#define MAXX 10007

int n;

int m;

int x;

int cnt[MAXX];

void init()
{
    mem(cnt, 0);
}






int main()
{
    init();

    int kases, kaseno = 0;

    sf("%d", &kases);

    while(kases--)
    {
        init();

        sf("%d", &n);

        m = n - 1;

        loop(i, n)
        {
            sf("%d", &x);

            if(x > m)
            {
                continue;
            }
            else
            {
                cnt[x]++;
                cnt[m-x]++;
            }
        }

        bool possible = true;

        loop(i, n)
        {
            if(cnt[i] != 2)
            {
                possible = false;
                break;
            }
        }

        pf("Case %d: ", ++kaseno);

        if(possible)
        {
            pf("yes\n");
        }
        else
        {
            pf("no\n");
        }

    }



    return 0;
}

(UVa) 222 – Budget Travel

0
/*
user: php
time: 0.012 sec
link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=158
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>

#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<sstream>



#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) for(int i=0; i<n; i++)
#define getint(n) scanf("%d", &n)
#define pb(a) push_back(a)
#define ll long long
#define SZ(a) int(a.size())
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define mem(a, v) memset(a, v, sizeof(a))
#define all(v) v.begin(), v.end()
#define pi acos(-1.0)
#define INF 10000000000
#define mod abs
#define pf printf
#define sf scanf

using namespace std;

#define MAXX 52 + 1

double final_dist;
double tank, miles_per_gallon;

double dist[MAXX];
double cost[MAXX];

double mincost[MAXX][MAXX];

int cnt;

double bfs_style()
{



    loop(i, cnt+1)
    {
        FOR(j, i, cnt+1)
        {
            mincost[i][j] = INF;
        }
    }

    mincost[0][0] = cost[0];



    for(int i=1; i<=cnt; i++)
    {
        double distance = dist[i];
        if(distance <= tank * miles_per_gallon)
        {
            mincost[0][i] = mincost[0][0];
        }


        loop(j, i)
        {
            distance = dist[i] - dist[j];
            if(2 * distance >= tank * miles_per_gallon || ( i < cnt && (dist[i+1] - dist[j])/miles_per_gallon > tank) )
                mincost[i][i] = min(mincost[i][i], mincost[j][i] + cost[i] * (distance/miles_per_gallon) );
        }



       // mincost[i][i] += (cost[i] * tank);

        if(mincost[i][i] >= INF) continue;

        mincost[i][i] += 200;


        for(int j=i+1; j<=cnt; j++)
        {
            distance = dist[j] - dist[i];

            if(distance <= tank * miles_per_gallon)
            {
                mincost[i][j] = mincost[i][i] ;
            }
        }
    }

    loop(i, cnt)
    {
        mincost[cnt][cnt] = min(mincost[cnt][cnt], mincost[i][cnt]);
    }

    return mincost[cnt][cnt];






}


int main()
{
    double result;
    int kaseno = 0;
    while(true)
    {
        cin>>final_dist;
        if(final_dist < 0) break;

        cin>>tank>>miles_per_gallon>>cost[0]>>cnt;
        cnt++;

        cost[0] = cost[0] * 100;
        dist[0] = 0;


        FOR(i, 1, cnt)
        {
            cin>>dist[i]>>cost[i];
        }

        dist[cnt] = final_dist;

        result = bfs_style() + 0.5;
        result = (ll) result;
        result /= 100.0;

        pf("Data Set #%d\nminimum cost = $%.2lf\n", ++kaseno, result);

    }

    return 0;
}






(CodeForces) C. k-Multiple Free Set

0
/*
user: hasib
link: http://codeforces.com/contest/275/problem/C
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>

#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>



#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) for(int i=0; i<n; i++)
#define getint(n) scanf("%d", &n)
#define pb(a) push_back(a)
#define ll long long
#define SZ size()
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define mem(a, v) memset(a, v, sizeof(a))
#define all(v) v.begin(), v.end()
#define pi acos(-1.0)
#define INF 1<<29
#define mod(a) (a>0?a:-a)

#define MAXX 100002

using namespace std;

int number_of_elements;
ll nums[MAXX];
ll k;

int low, high, mid;

bool search(ll n)
{
    low = 0;
    high = number_of_elements - 1;
    while(low <= high)
    {
        mid = (low+high)/2;
        if(nums[mid] == n)
        {
            break;
        }
        else if(n < nums[mid])
        {
            high = mid - 1;
        }
        else
        {
            low = mid + 1;
        }
    }

    if(low <= high)
        return true;
    else return false;
}


int main()
{

    getint(number_of_elements);
    cin>>k;

    loop(i, number_of_elements)
    {
        getint(nums[i]);
    }

    sort(nums, nums + number_of_elements);

    int taken = 0;
    ll a;
    map<int, bool>visited;

    loop(i, number_of_elements)
    {


        if( visited[nums[i]] == 0 )
        {
            a = nums[i];
            if(search(a*k))
            {
                if(search(a*k*k))
                {
                    visited[a*k] = 1; //nished
                    taken++;
                }
                else
                {
                    visited[a*k] = 1;
                    taken++;
                }
            }
            else
            {
                taken++;
            }

        }



    }

    cout<<taken<<endl;





    return 0;
}