logo

Sama summa ja XOR

Kokeile GfG Practicessa ' title= #practiceLinkDiv { näyttö: ei mitään !tärkeää; }

Positiivinen kokonaisluku n löytää positiivisten kokonaislukujen lukumäärän i siten, että 0<= i <= n and n+i = n^i 

Esimerkkejä:  

  Input :   n = 7   Output   : 1   Explanation:   7^i = 7+i holds only for only for i = 0 7+0 = 7^0 = 7   Input:   n = 12   Output:   4 12^i = 12+i hold only for i = 0 1 2 3 for i=0 12+0 = 12^0 = 12 for i=1 12+1 = 12^1 = 13 for i=2 12+2 = 12^2 = 14 for i=3 12+3 = 12^3 = 15
Recommended Practice Sama summa ja XOR Kokeile sitä!

Tapa 1 (yksinkertainen):  
Yksi yksinkertainen ratkaisu on iteroida kaikki i 0:n arvot<= i <= n and count all satisfying values.  



C++
/* C++ program to print count of values such  that n+i = n^i */ #include    using namespace std; // function to count number of values less than // equal to n that satisfy the given condition int countValues (int n) {  int countV = 0;  // Traverse all numbers from 0 to n and  // increment result only when given condition  // is satisfied.  for (int i=0; i<=n; i++ )  if ((n+i) == (n^i) )  countV++;  return countV; } // Driver program int main() {  int n = 12;  cout << countValues(n);  return 0; } 
Java
/* Java program to print count of values  such that n+i = n^i */ import java.util.*; class GFG {    // function to count number of values   // less than equal to n that satisfy  // the given condition  public static int countValues (int n)  {  int countV = 0;    // Traverse all numbers from 0 to n   // and increment result only when   // given condition is satisfied.  for (int i = 0; i <= n; i++ )  if ((n + i) == (n ^ i) )  countV++;    return countV;  }    /* Driver program to test above function */  public static void main(String[] args)   {  int n = 12;  System.out.println(countValues(n));    } } // This code is contributed by Arnav Kr. Mandal. 
Python3
# Python3 program to print count # of values such that n+i = n^i # function to count number # of values less than # equal to n that satisfy  # the given condition def countValues (n): countV = 0; # Traverse all numbers # from 0 to n and # increment result only # when given condition # is satisfied. for i in range(n + 1): if ((n + i) == (n ^ i)): countV += 1; return countV; # Driver Code n = 12; print(countValues(n)); # This code is contributed by mits 
C#
/* C# program to print count of values such that n+i = n^i */ using System; class GFG {    // function to count number of values   // less than equal to n that satisfy  // the given condition  public static int countValues (int n)  {  int countV = 0;    // Traverse all numbers from 0 to n   // and increment result only when   // given condition is satisfied.  for (int i = 0; i <= n; i++ )  if ((n + i) == (n ^ i) )  countV++;    return countV;  }    /* Driver program to test above function */  public static void Main()   {  int n = 12;  Console.WriteLine(countValues(n));    } } // This code is contributed by anuj_67. 
PHP
 // PHP program to print count // of values such that n+i = n^i // function to count number // of values less than // equal to n that satisfy  // the given condition function countValues ($n) { $countV = 0; // Traverse all numbers // from 0 to n and // increment result only // when given condition // is satisfied. for ($i = 0; $i <= $n; $i++ ) if (($n + $i) == ($n^$i) ) $countV++; return $countV; } // Driver Code $n = 12; echo countValues($n); // This code is contributed by m_kit ?> 
JavaScript
<script> /* JavaScript program to print count of values such that n+i = n^i */ // function to count number of values less than // equal to n that satisfy the given condition function countValues (n) {  let countV = 0;  // Traverse all numbers from 0 to n and  // increment result only when given condition  // is satisfied.  for (let i=0; i<=n; i++ )  if ((n+i) == (n^i) )  countV++;  return countV; } // Driver program  let n = 12;  document.write(countValues(n)); // This code is contributed by Surbhi Tyagi. </script> 

Lähtö:  

4

Aika monimutkaisuus: O(n)

Tilan monimutkaisuus: O(1)

Menetelmä 2 (tehokas): 
Tehokas ratkaisu on seuraava

tiedämme, että (n+i)=(n^i)+2*(n&i)
Joten n + i = n ^ i tarkoittaa n & i = 0
Tästä syystä ongelmamme pelkistyy i:n arvojen löytämiseen siten, että n & i = 0. Kuinka löytää tällaisten parien lukumäärä? Voimme käyttää unset-bittien määrää n:n binääriesityksessä. Jotta n & i olisi nolla, minun on poistettava kaikki n:n set-bitit. Jos k:s bitti on asetettu tiettyyn kohtaan n:ssä k:nnessä bitissä i:ssä, täytyy olla 0 aina muuten i:n k:s bitti voi olla 0 tai 1
Tällaisten yhdistelmien kokonaismäärä on siis 2^(määrittämättömien bittien määrä n:ssä)
Oletetaan esimerkiksi n = 12 (binaarinen esitys: 1 1 0 0). 
Kaikki i:n mahdolliset arvot, jotka voivat kumota n:n kaikki bitit, ovat 0 0 0/1 0/1, jossa 0/1 tarkoittaa joko 0:ta tai 1:tä. Tällaisten i:n arvojen lukumäärä on 2^2 = 4. 

Seuraava on yllä olevaa ideaa noudattava ohjelma.  

C++
/* c++ program to print count of values such  that n+i = n^i */ #include    using namespace std; // function to count number of values less than // equal to n that satisfy the given condition int countValues(int n) {  // unset_bits keeps track of count of un-set  // bits in binary representation of n  int unset_bits=0;  while (n)  {  if ((n & 1) == 0)  unset_bits++;  n=n>>1;  }  // Return 2 ^ unset_bits  return 1 << unset_bits; } // Driver code int main() {  int n = 12;  cout << countValues(n);  return 0; } 
Java
/* Java program to print count of values  such that n+i = n^i */ import java.util.*; class GFG {    // function to count number of values   // less than equal to n that satisfy   // the given condition  public static int countValues(int n)  {  // unset_bits keeps track of count  // of un-set bits in binary   // representation of n  int unset_bits=0;  while (n > 0)  {  if ((n & 1) == 0)  unset_bits++;  n=n>>1;  }    // Return 2 ^ unset_bits  return 1 << unset_bits;  }    /* Driver program to test above  function */  public static void main(String[] args)   {  int n = 12;  System.out.println(countValues(n));    } }   // This code is contributed by Arnav Kr. Mandal. 
C#
/* C# program to print count of values  such that n+i = n^i */ using System; public class GFG {    // function to count number of values   // less than equal to n that satisfy   // the given condition  public static int countValues(int n)  {    // unset_bits keeps track of count  // of un-set bits in binary   // representation of n  int unset_bits=0;  while (n > 0)  {  if ((n & 1) == 0)  unset_bits++;  n=n>>1;  }    // Return 2 ^ unset_bits  return 1 << unset_bits;  }    /* Driver program to test above  function */  public static void Main(String[] args)   {  int n = 12;  Console.WriteLine(countValues(n));    } } // This code is contributed by umadevi9616 
Python3
# Python3 program to print count of values such  # that n+i = n^i  # function to count number of values less than  # equal to n that satisfy the given condition  def countValues(n): # unset_bits keeps track of count of un-set  # bits in binary representation of n unset_bits = 0 while(n): if n & 1 == 0: unset_bits += 1 n = n >> 1 # Return 2 ^ unset_bits  return 1 << unset_bits # Driver code  if __name__=='__main__': n = 12 print(countValues(n)) # This code is contributed by rutvik 
PHP
 /* PHP program to print count  of values such that n+i = n^i */ // function to count number of  // values less than equal to n  // that satisfy the given  // condition function countValues( $n) { // unset_bits keeps track  // of count of un-set bits  // in binary representation // of n $unset_bits = 0; while ($n) { if (($n & 1) == 0) $unset_bits++; $n = $n >> 1; } // Return 2 ^ unset_bits return 1 << $unset_bits; } // Driver code $n = 12; echo countValues($n); // This code is contributed // by Anuj_67. ?> 
JavaScript
<script> // Javascript program to print count of values // such that n+i = n^i // Function to count number of values // less than equal to n that satisfy // the given condition function countValues(n) {    // unset_bits keeps track of count  // of un-set bits in binary  // representation of n  let unset_bits = 0;    while (n > 0)  {  if ((n & 1) == 0)  unset_bits++;    n = n >> 1;  }    // Return 2 ^ unset_bits  return 1 << unset_bits; }   // Driver Code let n = 12; document.write(countValues(n));   // This code is contributed by susmitakundugoaldanga   </script> 

Lähtö:  

4

Aika monimutkaisuus: O(log(n))

Tilan monimutkaisuus: O(1)

https://www.youtube.com/watch?v=zhu605v9KOI&feature=youtu.be