CSES - Datatähti 2020 alku - Results
Submission details
Task:Ruudukko
Sender:JPaananen
Submission time:2019-10-12 14:23:14 +0300
Language:C++ (C++17)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:44:2: error: 'scanf_s' was not declared in this scope
  scanf_s("%d", &n);
  ^~~~~~~
input/code.cpp:44:2: note: suggested alternative: 'scanf'
  scanf_s("%d", &n);
  ^~~~~~~
  scanf
input/code.cpp: In instantiation of 'void printGrid(int) [with int cl2 = 1]':
input/code.cpp:48:17:   required from here
input/code.cpp:33:6: warning: unused variable 'index' [-Wunused-variable]
  int index = 0;
      ^~~~~
input/code.cpp: In instantiation of 'void printGrid(int) [with int cl2 = 2]':
input/code.cpp:51:17:   required from here
input/code.cpp:33:6: warning: unused variable 'index' [-Wunused-variable]
input/code.cpp: In instantiation of 'void printGrid(int) [with int cl2 = 3]':
input/code.cpp:54:17:   required from here
input/code.cpp:33:6: warning: unused variable 'index' [-Wunused-variable]
input/code.cpp: In instantiation of 'void printGrid(int) [with int cl2 = 4]':
input/code.cpp:57:17:   required from here
input/code.cpp:33:6: wa...

Code

// Made by Jaakko P.

#include <stdio.h>

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

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

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

template<int cl2>
int magicFunc(int x, int y) {
	int val = 1;
	for (int i = 0; i < cl2; ++i)
		val += (((x >> i) + (y >> i)) & 1) << i;
	
	return val;
}


template<int cl2>
void printGrid(int size) {
	int index = 0;
	for (int i = 0; i < size; ++i) {
		for (int j = 0; j < size; ++j) {
			printf("%d ", magicFunc<cl2>(i, j));
		}
		puts("\n");
	}
}

int main() {
	int n;
	scanf_s("%d", &n);

	switch (log2(n-1)+1) { // TODO use ceil(log2(n)) instead
	case 1:
		printGrid<1>(n);
		break;
	case 2:
		printGrid<2>(n);
		break;
	case 3:
		printGrid<3>(n);
		break;
	case 4:
		printGrid<4>(n);
		break;
	case 5:
		printGrid<5>(n);
		break;
	case 6:
		printGrid<6>(n);
		break;
	case 7: // Stop at 2^7 because 2^7=128, 128 > 100.
		printGrid<7>(n);
		break;
	}

	return 0;
}