Representación de un número en potencias de otro

Dados dos números w y m, necesitamos determinar si es posible representar m en términos de potencias de w. Las potencias del número w se pueden sumar o restar para obtener my cada potencia de w se puede usar solo una vez.
Ejemplos: 
 

Input : 3 7
Output : Yes
As 7 = 9 - 3 + 1 (3^2 - 3^1 + 3^0 )
so it is possible .

Input : 100 50
Output : No
As 50 is less than 100 so we can never
represent it in the powers of 100 .

Aquí tenemos que representar m en términos de potencias de w usadas solo una vez para que pueda mostrarse a través de la siguiente ecuación. 
c0 + c1*w^1 + c2*w^2 + … = m —— (Ecuación 1)
Donde cada c0, c1, c2 … son -1 (para restar esa potencia de w ), 0 (sin usar esa potencia de w ), 1 (por sumar esa potencia de w ) .
=> c1*w^1 + c2*w^2 + … = m – c0 
=> w(c1 + c2*w^1 + c3*w^2 + … ) = m – c0 
=> c1 + c2*w ^1 + c3*w^2 + … = (m – c0)/w —— (Ecuación 2)
Ahora, observe la ecuación 1 y la ecuación 2: estamos tratando de resolver el mismo problema nuevamente. Entonces tenemos que recurrir hasta m > 0 . Para que exista tal solución (m — ci) debe ser un múltiplo de w, donde ci es el coeficiente de la ecuación . El ci puede ser -1, 0, 1 . Así que tenemos que verificar las tres posibilidades ( ( m – 1 ) % w == 0), ( ( m + 1 ) % w == 0) y ( m % w == 0) . Si no es así, entonces no habrá ninguna solución. 
 

C++

// CPP program to check if m can be represented
// as powers of w.
#include <bits/stdc++.h>
using namespace std;
 
bool asPowerSum(int w, int m)
{
    while (m) {
        if ((m - 1) % w == 0)
            m = (m - 1) / w;
       else if ((m + 1) % w == 0)
            m = (m + 1) / w;
         
        else if (m % w == 0)
            m = m / w;
         
        else
            break; // None of 3 worked.
    }
 
    // If m is not zero means, it can't be
    // represented in terms of powers of w.
    return (m == 0);
}
 
// Driver code
int main()
{
    int w = 3, m = 7;
    if (asPowerSum(w, m))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
   return 0;
}

Java

// Java program to check if m can
// be represented as powers of w.
 
class GFG
{
    static boolean asPowerSum(int w, int m)
    {
        while (m > 0)
        {
            if ((m - 1) % w == 0)
                m = (m - 1) / w;
             
            else if ((m + 1) % w == 0)
                m = (m + 1) / w;
             
            else if (m % w == 0)
                m = m / w;
             
            else
                break; // None of 3 worked.
        }
     
        // If m is not zero means, it can't be
        // represented in terms of powers of w.
        return (m == 0);
    }
     
    // Driver function
    public static void main (String[] args)
    {
        int w = 3, m = 7;
        if (asPowerSum(w, m))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Anant Agarwal.

Python3

# Python3 program to check if m can
# be represented as powers of w.
def asPowerSum(w, m):
    while (m > 0):
        if ((m - 1) % w == 0):
            m = (m - 1) / w;
         
        elif ((m + 1) % w == 0):
            m = (m + 1) / w;
         
        elif (m % w == 0):
            m = m / w;
         
        else:
            break; # None of 3 worked.
     
    # If m is not zero means, it can't be
    # represented in terms of powers of w.
    return (m == 0);
 
# Driver code
w = 3;
m = 7;
if (asPowerSum(w, m)):
    print("Yes");
else:
    print("No");
 
# This code is contributed by mits

C#

// C# program to check if
// m can be represented
// as powers of w.
using System;
 
class GFG
{
    static bool asPowerSum(int w,
                           int m)
    {
        while (m > 0)
        {
            if ((m - 1) % w == 0)
                m = (m - 1) / w;
             
            else if ((m + 1) % w == 0)
                m = (m + 1) / w;
             
            else if (m % w == 0)
                m = m / w;
             
            else
                break; // None of 3 worked.
        }
     
        // If m is not zero means,
        // it can't be represented
        // in terms of powers of w.
        return (m == 0);
    }
     
    // Driver Code
    static public void Main ()
    {
        int w = 3, m = 7;
        if (asPowerSum(w, m))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed
// by akt_mit.

PHP

<?php
// PHP program to check if m can
// be represented as powers of w.
 
function asPowerSum($w, $m)
{
    while ($m)
    {
        if (($m - 1) % $w == 0)
            $m = ($m - 1) / $w;
    else if (($m + 1) % $w == 0)
            $m = ($m + 1) / $w;
         
        else if ($m % $w == 0)
            $m = $m / $w;
         
        else
            break; // None of 3 worked.
    }
 
    // If m is not zero means, it can't be
    // represented in terms of powers of w.
    return ($m == 0);
}
 
// Driver code
$w = 3;
$m = 7;
if (asPowerSum($w, $m))
    echo "Yes\n";
else
    echo "No\n";
 
// This code is contributed by mits
?>

Javascript

<script>
 
// Javascript program to check if m can
// be represented as powers of w.
 
    function asPowerSum(w, m)
    {
        while (m > 0)
        {
            if ((m - 1) % w == 0)
                m = (m - 1) / w;
               
            else if ((m + 1) % w == 0)
                m = (m + 1) / w;
               
            else if (m % w == 0)
                m = m / w;
               
            else
                break; // None of 3 worked.
        }
       
        // If m is not zero means, it can't be
        // represented in terms of powers of w.
        return (m == 0);
    }
     
// Driver code
 
        let w = 3, m = 7;
        if (asPowerSum(w, m))
            document.write("Yes");
        else
            document.write("No");
       
      // This code is contributed by sanjoy_62.
</script>

Producción: 
 

Yes

Publicación traducida automáticamente

Artículo escrito por Surya Priy y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *