fork download
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <string.h>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <functional>
  9. #include <signal.h>
  10. #include <bitset>
  11.  
  12. using namespace std;
  13.  
  14. typedef unsigned long long uint64;
  15.  
  16. uint64 bitcount64(uint64 dw64){
  17. dw64 = ((dw64 & 0xAAAAAAAAAAAAAAAA) >> 1) + (dw64 & 0x5555555555555555);
  18. dw64 = ((dw64 & 0xCCCCCCCCCCCCCCCC) >> 2) + (dw64 & 0x3333333333333333);
  19. dw64 = ((dw64 & 0xF0F0F0F0F0F0F0F0) >> 4) + (dw64 & 0x0F0F0F0F0F0F0F0F);
  20. dw64 = ((dw64 & 0xFF00FF00FF00FF00) >> 8) + (dw64 & 0x00FF00FF00FF00FF);
  21. dw64 = ((dw64 & 0xFFFF0000FFFF0000) >> 16) + (dw64 & 0x0000FFFF0000FFFF);
  22. dw64 = ((dw64 & 0xFFFFFFFF00000000) >> 32) + (dw64 & 0x00000000FFFFFFFF);
  23.  
  24. return dw64;
  25. }
  26.  
  27. int highbit(uint64 ui64){
  28. int n = 0;
  29. while(ui64){
  30. ui64 = ui64 >> 1;
  31. n++;
  32. }
  33. return n;
  34. }
  35.  
  36. uint64 calc(uint64 a, uint64 b){
  37. return bitcount64(a) + bitcount64(b);
  38. }
  39.  
  40. void Solve(std::istream& stream, int num){
  41. std::string line;
  42. getline(stream, line);
  43. uint64 N;
  44. #ifdef _LINUX
  45. sscanf(line.c_str(), "%llu", &N);
  46. #else
  47. sscanf(line.c_str(), "%I64u", &N);
  48. #endif
  49.  
  50. // 答えの最大値は一番上のビットまで立っている状態…… なんじゃないかな?
  51. int hb = highbit(N);
  52. int max_ans = hb;
  53.  
  54. uint64 h = N;
  55. uint64 l = 0;
  56. uint64 half = N / 2 + 1;
  57.  
  58. uint64 current_max_ans = 0;
  59. for(int i = 0; i < half; i++){
  60. uint64 ans = calc(h, l);
  61. if(ans > current_max_ans){
  62. current_max_ans = ans;
  63. }
  64. h--;
  65. l++;
  66. }
  67.  
  68. #ifdef _LINUX
  69. printf("Case #%d: %llu\n", num, current_max_ans);
  70. #else
  71. printf("Case #%d: %I64u\n", num, current_max_ans);
  72. #endif
  73. }
  74.  
  75. void sig_handler(int sig){
  76. }
  77.  
  78. int main(int argc, char *argv[]){
  79. std::string line;
  80. int total_length;
  81. std::fstream stream(argv[1], std::ios::in);
  82. if(stream.fail())
  83. {
  84. printf("fail.\n");
  85. }
  86. // signal(SIGUSR1, sig_handler);
  87. {
  88. std::string line;
  89. getline(stream, line);
  90. sscanf(line.c_str(), "%d", &total_length);
  91. //printf("total_length: %d\n", total_length);
  92. }
  93. for(int n = 0; n < total_length; n++){
  94. Solve(stream, n+1);
  95. }
  96.  
  97. return 0;
  98. }
  99.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
3
9640
9885
6259
compilation info
prog.cpp:17: error: integer constant is too large for ‘long’ type
prog.cpp:17: error: integer constant is too large for ‘long’ type
prog.cpp:18: error: integer constant is too large for ‘long’ type
prog.cpp:18: error: integer constant is too large for ‘long’ type
prog.cpp:19: error: integer constant is too large for ‘long’ type
prog.cpp:19: error: integer constant is too large for ‘long’ type
prog.cpp:20: error: integer constant is too large for ‘long’ type
prog.cpp:20: error: integer constant is too large for ‘long’ type
prog.cpp:21: error: integer constant is too large for ‘long’ type
prog.cpp:21: error: integer constant is too large for ‘long’ type
prog.cpp:22: error: integer constant is too large for ‘long’ type
prog.cpp: In function ‘void Solve(std::istream&, int)’:
prog.cpp:47: warning: format ‘%I64u’ expects type ‘unsigned int*’, but argument 3 has type ‘uint64*’
prog.cpp:59: warning: comparison between signed and unsigned integer expressions
prog.cpp:71: warning: format ‘%I64u’ expects type ‘unsigned int’, but argument 3 has type ‘uint64’
prog.cpp:52: warning: unused variable ‘max_ans’
stdout
Standard output is empty