2025 暑訓小小賽超      題解

- 那個不是楊桃的星星

P.1 - 水餃.pdf

ISCOJ 4623 純輸出

正常的解

嗯它就長這樣

#include <iostream>
int main() {
  std::cout << "Hello, 2025 CKEFGISC summer camp!\n";
}

如何鴨腸

首先要知道最快的字串輸出方法是 write()

因為它是直接呼叫系統 (syscall) 進入最低階的 Kernal 執行

ssize_t write(int fileDescriptor, const void *buffer, size_t count);
syscall(1, 1, text, strlen(text));

我們可以用這個函數:

其中第一個 1 是 SYS_write、第二個 1 是 stdout

快速解

使用 write 加上抽象 exit 就可以把時間和記憶體壓縮到最小

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,abm,avx")
#include <string.h>
#define out(m) syscall(1, 1, m, strlen(m))
#define exit(k) syscall(0x3c, k)
int main() {
	out("Hello, 2025 CKEFGISC summer camp!");
	exit(0);
}

記得編譯器要選 C-99

P.2 - 水餃睡覺

ISCOJ 4624 簡單加減法

正常的解

不難吧

#include <iostream> 
int main() {
  long long a, b;
  char c;
  std::cin >> a >> c >> b;
  std::cout << (c == '+' ? a + b : a - b) << "\n";
}

如何鴨腸

網路上有許多快讀快寫模板 啊這個是我剛才隨便寫的

void in(long long* num) {
  *num = 0;
  char c;
  do {
    c = getchar_unlocked();
    if (c == '-') {
      in(num);
      *num = -*num;
      return;
    }
  } while (c < '0' || c > '9');
  while (c >= '0' && c <= '9') {
    *num = (*num * 10) + (c - '0');
    c = getchar_unlocked();
  }
}
void out(long long x) {
  if (x == 0) {
    putchar_unlocked('0');
    return;
  }
  if (x < 0) {
    putchar_unlocked('-');
    x = -x;
  }
  char buf[20];
  char i = 0;
  while (x > 0) {
    buf[i++] = (x % 10) + '0';
    x /= 10;
  }
  while (i--) {
    putchar_unlocked(buf[i]);
  }
}

快速解

其實這個沒有很快但隨便啦

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,abm,avx")
void in(long long* num) {
  *num = 0;
  char c;
  do {
    c = getchar_unlocked();
    if (c == '-') {
      in(num);
      *num = -*num;
      return;
    }
  } while (c < '0' || c > '9');
  while (c >= '0' && c <= '9') {
    *num = (*num * 10) + (c - '0');
    c = getchar_unlocked();
  }
}
void out(long long x) {
  if (x == 0) {
    putchar_unlocked('0');
    return;
  }
  if (x < 0) {
    putchar_unlocked('-');
    x = -x;
  }
  char buf[20];
  char i = 0;
  while (x > 0) {
    buf[i++] = (x % 10) + '0';
    x /= 10;
  }
  while (i--) {
    putchar_unlocked(buf[i]);
  }
}
int main() {
	long long a, b;
  in(&a);
  char c = getchar_unlocked();
  in(&b);
  out(c == '+' ? a + b : a - b);
}

P.3 - 關於禮物

ISCOJ 4625 二進位轉換

正常的解

就這東西其實是有內建的 bitset 可以用

#include <iostream>
#include <bitset>
#include <string>
int main() {
  unsigned int n;
  std::cin >> n;
  std::string bin = std::bitset<32>(n).to_string();
  bin.erase(0, bin.find_first_of('1'));
  if (bin.empty()) bin = "0";
  std::cout << bin << "\n";
}

不過記得要把前面多的 0 去掉

快速解

跟上一題基本上是同個做法 只是輸出稍微簡單一點

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,abm,avx")
void in(int* num) {
  *num = 0;
  char c = getchar_unlocked();
  while (c < '0' || c > '9') {
    c = getchar_unlocked();
  }
  while (c >= '0' && c <= '9') {
    *num = (*num * 10) + (c - '0');
    c = getchar_unlocked();
  }
}
int main(void) {
  int n;
  in(&n);
  if (n == 0) {
    putchar_unlocked('0');
    return 0;
  }

  char buf[32];
  int i = 0;

  while (n) {
    buf[i++] = (n & 1) ? '1' : '0';
    n >>= 1;
  }
  while (i--) {
    putchar_unlocked(buf[i]);
  }
}

P.4 - 圓周運動需要什麼力

ISCOJ 4626 國中數學

正常的解

就開心算數學

#include <iostream>
#include <cmath> 
int main() {
  long long a, b, c, d;
  std::cin >> a >> b >> c;
  d = b * b - 4 * a * c;
  if (d < 0) {
  	std::cout << "Not for junior high school\n";
  }
  else {
    std::cout << ((-b) + sqrt(d)) / (2 * a)
      << " " 
      << ((-b) - sqrt(d)) / (2 * a) 
      << '\n';
  }
}

整數開根

其實我不確定自己刻會不會比較快

long long floorSqrt(long long n) {
  if (n < 0) return -1;
  if (n == 0 || n == 1) return n;

  long long low = 1, high = n, ans = 1;
  while (low <= high) {
    long long mid = low + (high - low) / 2;
    if (mid <= n / mid) {
      ans = mid;
      low = mid + 1;
    } 
    else {
      high = mid - 1;
    }
  }
  return ans;
}

快速解

簡單吧

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,abm,avx")
#define outString(m) syscall(1, 1, m, strlen(m))
void in(long long* num) {
  *num = 0;
  char c;
  do {
    c = getchar_unlocked();
    if (c == '-') {
      in(num);
      *num = -*num;
      return;
    }
  } while (c < '0' || c > '9');
  while (c >= '0' && c <= '9') {
    *num = (*num * 10) + (c - '0');
    c = getchar_unlocked();
  }
}
void out(long long x) {
  if (x == 0) {
    putchar_unlocked('0');
    return;
  }
  if (x < 0) {
    putchar_unlocked('-');
    x = -x;
  }
  char buf[20];
  char i = 0;
  while (x > 0) {
    buf[i++] = (x % 10) + '0';
    x /= 10;
  }
  while (i--) {
    putchar_unlocked(buf[i]);
  }
}
long long floorSqrt(long long n) {
  if (n < 0) return -1;
  if (n == 0 || n == 1) return n;

  long long low = 1, high = n, ans = 1;
  while (low <= high) {
    long long mid = low + (high - low) / 2;
    if (mid <= n / mid) {
      ans = mid;
      low = mid + 1;
    } 
    else {
      high = mid - 1;
    }
  }
  return ans;
}
int main() {
  long long a, b, c, d;
  in(&a); in(&b); in(&c);
  d = floorSqrt(b * b - 4 * a * c);
  if (d == -1) {
    outString("Not for junior high school");
  }
  else {
    out(((-b) + d) / (2 * a));
    putchar_unlocked(' ');
    out(((-b) - d) / (2 * a));
  }
}

P.5 - 你有貓餅

ISCOJ 4626 輕鬆的小判斷

正常的解

就用字串

#include <iostream>
int main() {
    std::string str;
    std::cin >> str;
    int tail = str.back() - '0';
    std::cout << (tail & 1 ? "odd" : "even") << "\n";
    return 0;
}

快速解

就一直吃字元

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,abm,avx")
#include <string.h>
#define out(m) syscall(1, 1, m, strlen(m))
#define exit(k) syscall(0x3c, k)
int main() {
  char p, c;
  do {
    p = c;
    c = getchar_unlocked();
  } while (c != '\n' && c != -1);

  if (p & 1) // '0' = 48, is even
    out("odd");
  else
    out("even");
  exit(0);
}

再見

顯然晴☆不是最毒的

鴨腸界人外有人 天外有天哪

2025 暑訓小小賽超 🉐 題解

By 晴☆

2025 暑訓小小賽超 🉐 題解

如何鴨腸

  • 43