1. 程式人生 > 其它 >CSP 2021 複習

CSP 2021 複習

對拍 (linux)

idea

首先需要正解(你認為的)編譯生成的程式 std 和暴力程式碼的編譯生成的程式 slow.

在同一資料夾寫出 random.cpp 自動生成資料為 q.in, 這裡補充較好的 srand() 方法.

srand(clock()); // clock() 獲取當前程式執行時間, 返回 long, 包含於 time.h
srand(time(0));

然後編寫程式 checker.cpp, 進行對拍.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

inline void sovle (int t) {
	printf("=== Case %d ===\n", t);
	system("./random");	// 生成資料
	system("./slow");	// 執行暴力 
	long s1 = clock();
	system("./std");		// 執行正解 
	long s2 = clock(); 
	if (system("diff std.out slow.out"))
		printf("WA\n");
	else
		printf("AC\n");
	printf("%ldms\n\n", s2 - s1); 
}
int main () {
	int t = 30;
	for (int i = 1; i <= t; i++)
		sovle(i);
	return 0;
}

常用技巧

__int128

利用 __int128 解決 \(a+b\) 問題

#include <stdio.h>
#define lll __int128
#define ulll __uint128_t

inline lll read () {
	char ch = getchar();
	lll ret = 0, c = 1;
	while (1) {
		if (ch >= '0' && ch <= '9')
			break;
		if (ch == '-')
			c = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		ret = ret * 10 + ch - '0';
		ch = getchar();
	}
	ret = ret * c;
	return ret;
}

inline void write (lll x) {
	if (x < 0)	printf("-"), x = -x;
	if (x > 9)
		write(x / 10);
	putchar(x % 10 + '0');
}

int main () {
	lll x, y;	// sovle the a + b problem by __int128
	x = read(), y = read();
	write(x + y);
	return 0;
}

快速讀入與快速輸出

見 __int128 部分

register

已經沒用了, 哭哭.

( ゚∀゚)o彡゜ ヒーコー ヒーコー!