brute force implementation math number theory *1700

Please click on ads to support us..

C++ Code:

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

#define ll long long int
#define endl "\n"
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;

#define vec(name) vector<ll> name;
#define all(vector) vector.begin(), vector.end()
#define sortde(vector) sort(all(vector), greater<ll>());

#define invecll(n, name)       \
    vector<ll> name;           \
    for (ll i = 0; i < n; i++) \
    {                          \
        ll lwjfh;              \
        cin >> lwjfh;          \
        name.push_back(lwjfh); \
    }

#define vecp(vector)                       \
    for (ll i = 0; i < vector.size(); i++) \
        cout << vector[i] << " ";          \
    cout << endl;

#define inll(name) \
    ll name;       \
    cin >> name;

#define ins(name) \
    string name;  \
    cin >> name;

ll sumvec(vector<ll> v);
bool isPrime(ll n);
ll binpow(ll a, ll b);

ll fin(ll k, ll n)
{

    if (n < k)
        return 0;
    if (n == k)
        return 1;

    ll ans = 0;

    for (ll i = 0; i < k; i++)
    {
        ans += fin(k, n - i - 1);
    }

    return ans;
}


// ----------------x------------------------x------------------------------x----------------------

//

// -----------------------x----------------------------------x------------------------------------

void solve()
{
    inll(n) inll(b)

    ll temp = n;
    n = b;

    map<ll,ll> mp;
    while (n % 2 == 0)
    {
        mp[2]++;
        n = n/2;
    }
 
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
           mp[i]++;
            n = n/i;
        }
    }
 
  
    if (n > 2)mp[n]++;

   ll ans =-1;
    for(auto itr : mp){

        ll mod = itr.second;
        ll num = 1;
        ll largest = itr.first;
        ll as = 0;

   while(num <=( temp/largest)){

        num*= largest;
        as+= (temp/num);
   }

   
   if(ans == -1) ans = as/mod; else 
    ans = min(ans,as/mod);
    }


   cout<<ans<<endl;
   

    
    
}

// ----------------x-----------------main---------------------------x----------------------------

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

#endif

    ll test;
    test = 1;
    // cin >> test;

    for (ll i = 1; i <= test; i++)
    {

        solve();
    }

    return 0;
}

// ----------------x---------------x----------------------------x----------------------------

//

// ----------------x-----------------------x-------------------------------------x-----------

// functions

ll sumvec(vector<ll> v)
{

    ll sum = 0;

    for (ll i = 0; i < v.size(); i++)
    {
        sum += v[i];
    }

    return sum;
}

bool isPrime(ll n)
{

    if (n == 0 || n == 1)
    {
        return false;
    }
    if (n == 2)
    {
        return true;
    }
    
    for (ll i = 3; i*i <= n; i+=2)
    {
        if(n%i ==0) return false;
    }

    return true;
    
}

ll binpow(ll a, ll b)
{
    if (b == 0)
        return 1;
    ll res = binpow(a, b / 2);
    if (b % 2 == 0)
        return (res * res);
    else
        return (res * res * a);
}


Comments

Submit
0 Comments
More Questions

869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference