Project Euler #32: Pandigital products

  • + 0 comments

    You need two nested loops from 1 to 10000 . Then for each i and j , check i, j and i*j.

    c++ Solution :

    #include <bits/stdc++.h>
    using namespace std;
    #define all(v) (v).begin(), (v).end()
    #define debug(x) cout << #x << " = " << x << endl
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    inline ll Mod(ll x, ll mod) { return x % mod >= 0 ? x % mod : x % mod + mod; }
    
    bool is_panddigital(ll a, ll b, int n)
    {
        bool mark[10] = {0};
        ll y = a * b;
        while (a)
        {
            int u = a % 10;
            a /= 10;
            if (mark[u] || u > n || u == 0)
            {
                return 0;
            }
            mark[u] = 1;
        }
        while (b)
        {
            int u = b % 10;
            b /= 10;
            if (mark[u] || u > n || u == 0)
            {
                return 0;
            }
            mark[u] = 1;
        }
        while (y)
        {
            int u = y % 10;
            y /= 10;
            if (mark[u] || u > n || u == 0)
            {
                return 0;
            }
            mark[u] = 1;
        }
        for (int i = 1; i <= n; i++)
        {
            if (!mark[i])
                return 0;
        }
        return 1;
    }
    
    int main()
    {
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        set<int> ans;
        int n;
        cin >> n;
        for (int i = 1; i <= 10000; i++)
        {
            for (int j = 1; j <= 10000; j++)
            {
                if (is_panddigital(i, j, n))
                    ans.insert(i * j);
            }
        }
        ll k = 0;
        for (int i : ans)
            k += i;
        cout << k;
    }