CSES - Datatähti 2020 alku - Results
Submission details
Task:Lukuvälit
Sender:JPaananen
Submission time:2019-10-13 22:11:02 +0300
Language:C++17
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'std::__cxx11::string toBinary(long long int)':
input/code.cpp:22:19: error: 'log2' was not declared in this scope
  long len = (long)log2(l);
                   ^~~~
input/code.cpp:22:19: note: suggested alternative: 'ilog2'
  long len = (long)log2(l);
                   ^~~~
                   ilog2
input/code.cpp:31:7: warning: operation on 'len' may be undefined [-Wsequence-point]
   str[--len] = (l & (1 << len)) + '0';
       ^~~~~
input/code.cpp: In function 'long int ilog10(long long int)':
input/code.cpp:73:9: error: 'log10' was not declared in this scope
  return log10(x);
         ^~~~~
input/code.cpp:73:9: note: suggested alternative: 'ilog10'
  return log10(x);
         ^~~~~
         ilog10
input/code.cpp: At global scope:
input/code.cpp:72:13: warning: 'long int ilog10(long long int)' defined but not used [-Wunused-function]
 static long ilog10(long long x) {
             ^~~~~~

Code

#include <iostream>
#include <string>
#include <algorithm>

#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
int ilog2(int x) {
	return 31 ^ __builtin_clz(x);
}

#elif defined(_MSC_VER)
#include <intrin.h>

int ilog2(int x) {
	unsigned long r = 0;
	_BitScanReverse(&r, x);
	return r;
}
#endif

std::string toBinary(long long l) {
	long len = (long)log2(l);

	std::string str;
	str.resize(len);

	// log2(127 = 111 1111) = 6
	// log2(128 = 1000 0000) = 7

	while (l) {
		str[--len] = (l & (1 << len)) + '0';
	}
	return str;
}

int countBinary(const std::string& line) {
	int index = 0;
	int len = line.length();

	while (line[index++] != ' ') {}
	int lbLen = index - 1;
	int shift = lbLen;

	long long lb = 0; // lower bound
	for (int i = 0; i < lbLen; ++i) {
		char ch = line[i] - '0';
		
		if (ch > 1) {
			lb += 1ll << shift;
			break;
		}
		lb |= (long long)(ch & 1) << (--shift);
	}

	//std::cout << "lbLen: " << lbLen << std::endl;
	//std::cout << "lb: " << lb << " = " << toBinary(lb) << std::endl;

	long long ub = 0; // upper bound
	while (index != len) {
		ub <<= 1;
		ub |= (line[index++] != '0');
	}

	//std::cout << "ub: " << toBinary(ub) << std::endl;

	//std::cout << lb << ", " << ub << std::endl;

	return ub - lb + 1;
}


static long ilog10(long long x) {
	return log10(x);
	//static const unsigned char guess[33] = {
	//	0, 0, 0, 0, 1, 1, 1, 2, 2, 2,
	//	3, 3, 3, 3, 4, 4, 4, 5, 5, 5,
	//	6, 6, 6, 6, 7, 7, 7, 8, 8, 8,
	//	9, 9, 9
	//};
	//static const unsigned int tenToThe[] = {
	//	1, 10, 100, 1000, 10000, 100000,
	//	1000000, 10000000, 100000000, 1000000000,
	//};
	//unsigned int digits = guess[ilog2(x)];
	//return digits + (x >= tenToThe[digits]);
}

int main() {

	//while (true) {
	//	int i;
	//	std::cin >> i;

	//	int j = i;// < 10 ? 0 : i - 10;
	//	std::cout << "log2(" << j << ") = " << ilog10(j) << std::endl;
	//}

	
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int n;
	std::cin >> n;
	std::cin.ignore(); // Skip newline

	std::string line;
	for (int i = 0; i < n; ++i) {
		std::getline(std::cin, line);

		std::cout << countBinary(line) << "\n";
	}
}