fork download
  1. #!/usr/bin/ruby
  2. if RUBY_PLATFORM=~/linux/
  3. if true
  4. require 'fiddle'
  5. __popcount_fn=Fiddle::Function.new(Fiddle::Handle::DEFAULT['__sched_cpucount'],[Fiddle::TYPE_INT,Fiddle::TYPE_VOIDP],Fiddle::TYPE_INT)
  6. define_method(:popcount){|n|__popcount_fn.call(8,[n].pack('q'))}
  7. else
  8. require 'fiddle/import'
  9. module LibC
  10. extend Fiddle::Importer
  11. dlload 'libc.so.6'
  12. extern 'int __sched_cpucount(int,long long*)'
  13. end
  14. def popcount(n) LibC.__popcountdi2(8,[n]) end
  15. end
  16. elsif RUBY_PLATFORM=~/darwin/
  17. if true
  18. require 'fiddle'
  19. __popcount_fn=Fiddle::Function.new(Fiddle::Handle::DEFAULT['__popcountdi2'],[Fiddle::TYPE_LONG],Fiddle::TYPE_INT)
  20. define_method(:popcount){|n|__popcount_fn.call(n)}
  21. else
  22. require 'fiddle/import'
  23. module LibC
  24. extend Fiddle::Importer
  25. dlload 'libSystem.dylib'
  26. extern 'int __popcountdi2(long)'
  27. end
  28. def popcount(n) LibC.__popcountdi2(n) end
  29. end
  30. else
  31. def popcount(n) n==0 ? 0 : popcount(n/2)+n%2 end
  32. end
  33.  
  34. p 1<<popcount(gets.to_i)
Success #stdin #stdout 0.05s 9872KB
stdin
98765432
stdout
8192