From 2f674dc80f0382f1c3178f435714960734dc9d3c Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sat, 6 Jun 2020 21:33:13 +0700 Subject: Reorganize stuff from secondary school --- 2ndary/12/TP-HN-2009/R1/BTN.PAS | 87 +++++++++++++++++++++++++++++++++++++ 2ndary/12/TP-HN-2009/R1/HEXA.PAS | 75 ++++++++++++++++++++++++++++++++ 2ndary/12/TP-HN-2009/R1/PS.PAS | 80 ++++++++++++++++++++++++++++++++++ 2ndary/12/TP-HN-2009/R1/R1.pdf | Bin 0 -> 65142 bytes 2ndary/12/TP-HN-2009/R2/BAI1.PAS | 90 +++++++++++++++++++++++++++++++++++++++ 2ndary/12/TP-HN-2009/R2/BAI2.CPP | 58 +++++++++++++++++++++++++ 2ndary/12/TP-HN-2009/R2/BAI3.PAS | 87 +++++++++++++++++++++++++++++++++++++ 2ndary/12/TP-HN-2009/R2/R2.pdf | Bin 0 -> 103168 bytes 8 files changed, 477 insertions(+) create mode 100644 2ndary/12/TP-HN-2009/R1/BTN.PAS create mode 100644 2ndary/12/TP-HN-2009/R1/HEXA.PAS create mode 100644 2ndary/12/TP-HN-2009/R1/PS.PAS create mode 100644 2ndary/12/TP-HN-2009/R1/R1.pdf create mode 100644 2ndary/12/TP-HN-2009/R2/BAI1.PAS create mode 100644 2ndary/12/TP-HN-2009/R2/BAI2.CPP create mode 100644 2ndary/12/TP-HN-2009/R2/BAI3.PAS create mode 100644 2ndary/12/TP-HN-2009/R2/R2.pdf (limited to '2ndary/12/TP-HN-2009') diff --git a/2ndary/12/TP-HN-2009/R1/BTN.PAS b/2ndary/12/TP-HN-2009/R1/BTN.PAS new file mode 100644 index 0000000..dbb022d --- /dev/null +++ b/2ndary/12/TP-HN-2009/R1/BTN.PAS @@ -0,0 +1,87 @@ +type sni = record + s : ansistring; + n : integer +end; + +var + f : text; + s : ansistring; + op, cl : integer; + c : char; + +function cal(s : ansistring) : integer; + var + c : char; + tmp : integer = 0; + begin + cal := 0; + for c in s do + if c = '(' then + begin + inc(tmp); + if tmp > cal then + cal := tmp + end + else + begin + dec(tmp); + if tmp < 0 then exit(0) + end; + if tmp <> 0 then + exit(0) + end; + +function rplc( + s : ansistring; + c : char; + idx : integer +) : ansistring; + begin + exit(copy(s, 1, idx - 1) + c + copy(s, idx + 1, length(s) - idx + 1)) + end; + +function libtn( + s : ansistring; + op, cl, idx : integer +) : sni; + var + i : integer; + v0, v1 : sni; + begin + if (op = 0) and (cl = 0) then + begin + libtn.s := s; + libtn.n := cal(s); + exit + end; + i := idx; + while s[i] <> '?' do + inc(i); + if op = 0 then + exit(libtn(rplc(s, ')', i), 0, cl - 1, i + 1)); + if cl = 0 then + exit(libtn(rplc(s, '(', i), op - 1, 0, i + 1)); + v0 := libtn(rplc(s, '(', i), op - 1, cl, i + 1); + v1 := libtn(rplc(s, ')', i), op, cl - 1, i + 1); + if v0.n > v1.n then + exit(v0) + else exit(v1) + end; + +begin + assign(f, 'BTN.INP'); + reset(f); + read(f, s); + close(f); + op := length(s) div 2; + cl := length(s) div 2; + for c in s do + if c = '(' then + dec(op) + else if c = ')' then + dec(cl); + assign(f, 'BTN.OUT'); + rewrite(f); + writeln(f, libtn(s, op, cl, 1).s); + close(f) +end. diff --git a/2ndary/12/TP-HN-2009/R1/HEXA.PAS b/2ndary/12/TP-HN-2009/R1/HEXA.PAS new file mode 100644 index 0000000..a2fd6ca --- /dev/null +++ b/2ndary/12/TP-HN-2009/R1/HEXA.PAS @@ -0,0 +1,75 @@ +var + f : text; + n, n0, m, i, j, tmp : longint; + s : string; + +function dec2hex(deca : longint) : string; + var + a : array[0..7] of byte; + i, j : byte; + begin + dec2hex := ''; + i := 0; + while deca > 0 do + begin + a[i] := deca mod 16; + deca := deca div 16; + inc(i) + end; + dec(i); + for j := i downto 0 do + case a[j] of + 0 : dec2hex := dec2hex + '0'; + 1 : dec2hex := dec2hex + '1'; + 2 : dec2hex := dec2hex + '2'; + 3 : dec2hex := dec2hex + '3'; + 4 : dec2hex := dec2hex + '4'; + 5 : dec2hex := dec2hex + '5'; + 6 : dec2hex := dec2hex + '6'; + 7 : dec2hex := dec2hex + '7'; + 8 : dec2hex := dec2hex + '8'; + 9 : dec2hex := dec2hex + '9'; + 10 : dec2hex := dec2hex + 'A'; + 11 : dec2hex := dec2hex + 'B'; + 12 : dec2hex := dec2hex + 'C'; + 13 : dec2hex := dec2hex + 'D'; + 14 : dec2hex := dec2hex + 'E'; + 15 : dec2hex := dec2hex + 'F' + end + end; + +begin + assign(f, 'HEXA.INP'); + reset(f); + read(f, n); + close(f); + m := n; + i := 0; + while m > 0 do + begin + inc(i); + tmp := 1; + for j := 1 to i - 1 do tmp := tmp * 16; + m := m + i * (tmp - tmp * 16) + end; + m := i; + for i := 1 to m - 1 do + begin + tmp := 1; + for j := 1 to i - 1 do tmp := tmp * 16; + n := n + i * (tmp - tmp * 16) + end; + n0 := (n + m - 1) div m; + for i := 1 to m - 1 do + begin + tmp := 1; + for j := 1 to i - 1 do tmp := tmp * 16; + n0 := n0 + tmp * 16 - tmp + end; + s := dec2hex(n0); + if n mod m > 0 then m := n mod m; + assign(f, 'HEXA.OUT'); + rewrite(f); + writeln(f, s[m]); + close(f) +end. diff --git a/2ndary/12/TP-HN-2009/R1/PS.PAS b/2ndary/12/TP-HN-2009/R1/PS.PAS new file mode 100644 index 0000000..6cf2d09 --- /dev/null +++ b/2ndary/12/TP-HN-2009/R1/PS.PAS @@ -0,0 +1,80 @@ +var + f : text; + m, n : byte; + k, i, j, l, gcd0 : integer; + a, b : array[1..30] of integer; + c : array[0..1, 1..900] of integer; + +function gcd(d, e : integer) : integer; + var tmp : integer; + begin + while d > 0 do + begin + tmp := d; + d := e mod d; + e := tmp + end; + gcd := e + end; + +procedure qsort(b, e: integer); + var i, j, x, tmp: integer; + begin + i := b; + j := e; + x := (b + e) div 2; + repeat + while c[0, i] * c[1, x] < c[0, x] * c[1, i] do inc(i); + while c[0, x] * c[1, j] < c[0, j] * c[1, x] do dec(j); + if i <= j then + begin + tmp := c[0, i]; + c[0, i] := c[0, j]; + c[0, j] := tmp; + tmp := c[1, i]; + c[1, i] := c[1, j]; + c[1, j] := tmp; + inc(i); + dec(j) + end + until i > j; + if b < j then qsort(b, j); + if i < e then qsort(i, e) + end; + +begin + assign(f, 'PS.INP'); + reset(f); + read(f, m, n, k); + for i := 1 to m do read(f, a[i]); + for i := 1 to n do read(f, b[i]); + close(f); + l := 0; + for i := 1 to m do + for j := 1 to n do + begin + inc(l); + gcd0 := gcd(a[i], b[j]); + c[0, l] := a[i] div gcd0; + c[1, l] := b[j] div gcd0 + end; + qsort(1, l); + i := 1; + while i < l do + begin + inc(i); + if c[0, i] * c[1, i - 1] = c[0, i - 1] * c[1, i] then + begin + dec(l); + for j := i to l do + begin + c[0, j] := c[0, j + 1]; + c[1, j] := c[1, j + 1] + end + end + end; + assign(f, 'PS.OUT'); + rewrite(f); + writeln(f, c[0, k], ' ', c[1, k]); + close(f) +end. diff --git a/2ndary/12/TP-HN-2009/R1/R1.pdf b/2ndary/12/TP-HN-2009/R1/R1.pdf new file mode 100644 index 0000000..b0834b3 Binary files /dev/null and b/2ndary/12/TP-HN-2009/R1/R1.pdf differ diff --git a/2ndary/12/TP-HN-2009/R2/BAI1.PAS b/2ndary/12/TP-HN-2009/R2/BAI1.PAS new file mode 100644 index 0000000..b28ae0b --- /dev/null +++ b/2ndary/12/TP-HN-2009/R2/BAI1.PAS @@ -0,0 +1,90 @@ +uses math; + +var + f : text; + n : longint; + lena, i : integer; + a : array[1..512] of longint; + +procedure insort(x : longint); + var i, j : integer; + begin + inc(lena); + a[lena] := x - 1; + for i := 1 to lena do + if a[i] < x then begin + for j := lena downto i + 1 do + a[j] := a[j - 1]; + a[i] := x; + exit + end + end; + +function notin(x : longint) : boolean; + var l, h, m : integer; + begin + l := 1; + h := lena; + repeat + m := (l + h) div 2; + if a[m] = x then exit(false); + if a[m] < x then h := m - 1 + else l := m + 1 + until l > h; + notin := true + end; + +procedure mklist(n0 : longint); + var + i, j : byte; + n10, m0 : longint; + begin + if n0 <> 0 then begin + insort(n0); + for i := 0 to trunc(log10(n0)) do + begin + n10 := 1; + for j := 1 to i do n10 := n10 * 10; + m0 := n0 div (n10 * 10) + n0 mod n10; + if notin(m0) then mklist(m0) + end + end + end; + +function prime(m : longint) : boolean; + var p, q : integer; + begin + if m < 2 then exit(false); + if m = 2 then exit(true); + if m = 3 then exit(true); + if m mod 2 = 0 then exit(false); + if m mod 3 = 0 then exit(false); + p := 5; + q := 2; + while p * p <= m do + begin + if m mod p = 0 then exit(false); + p := p + q; + q := 6 - q + end; + prime := true + end; + +begin + assign(f, 'BAI1.INP'); + reset(f); + read(f, n); + close(f); + lena := 0; + mklist(n); + assign(f, 'BAI1.OUT'); + rewrite(f); + for i := 1 to lena do + if prime(a[i]) then begin + writeln(f, a[i]); + close(f); + exit + end; + writeln(f, -1); + close(f) +end. diff --git a/2ndary/12/TP-HN-2009/R2/BAI2.CPP b/2ndary/12/TP-HN-2009/R2/BAI2.CPP new file mode 100644 index 0000000..a47760f --- /dev/null +++ b/2ndary/12/TP-HN-2009/R2/BAI2.CPP @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include + +#define ENC(distance, current, coupon) (((distance) << 14) + ((current) << 1) + coupon) +#define DEC_D(data) ((data) >> 14) +#define DEC_C(data) ((data) >> 1 & 0b1111111111111) +#define DEC_B(data) ((data) & 1) + +using namespace std; + +int +main() +{ + ifstream infile; + infile.open("BAI2.INP"); + int n, m; + infile >> n >> m; + map> c; + long long k, i, j, l; + for (k = 0; k < m; k++) + { + infile >> i >> j >> l; + c[i][j] = l; + } + infile.close(); + + priority_queue, greater> q; + for (auto& item : c[1]) + { + q.push(ENC(item.second, item.first, 1)); + q.push(ENC(0, item.first, 0)); + } + + long long tmp; + while (!q.empty()) + { + tmp = q.top(); + q.pop(); + if (DEC_C(tmp) == n) + break; + for (auto& item : c[DEC_C(tmp)]) + q.push(ENC(DEC_D(tmp) + item.second, item.first, DEC_B(tmp))); + if (DEC_B(tmp)) + for (auto& item : c[DEC_C(tmp)]) + q.push(ENC(DEC_D(tmp), item.first, 0)); + } + + ofstream outfile; + outfile.open("BAI2.OUT"); + outfile << DEC_D(tmp) << endl; + outfile.close(); + + return 0; +} diff --git a/2ndary/12/TP-HN-2009/R2/BAI3.PAS b/2ndary/12/TP-HN-2009/R2/BAI3.PAS new file mode 100644 index 0000000..30eecdf --- /dev/null +++ b/2ndary/12/TP-HN-2009/R2/BAI3.PAS @@ -0,0 +1,87 @@ +type + ar = array of ansistring; + +var + f : text; + s0 : ansistring; + a0 : ar; + +function incre(s1, s2 : ansistring) : boolean; + var + i : smallint; + begin + if length(s1) < length(s2) then exit(true); + if length(s1) > length(s2) then exit(false); + for i := 1 to length(s1) do + begin + if ord(s1[i]) < ord(s2[i]) then exit(true); + if ord(s1[i]) > ord(s2[i]) then exit(false) + end; + exit(false) + end; + +function cal(a : ar) : smallint; + var + len, i, tmp : smallint; + begin + cal := 0; + i := 0; + len := length(a); + while (cal + i < len) and (i + 1 < len) do + begin + inc(i); + if incre(a[0], a[i]) then + begin + tmp := cal(copy(a, i, len - i)) + 1; + if tmp > cal then + cal := tmp + end + end + end; + +function putin( + aray : ar; + strng : ansistring; + len : smallint +) : ar; + begin + setlength(aray, length(aray) + 1); + aray[length(aray) - 1] := copy(strng, 1, len); + exit(aray) + end; + +function libai3( + s : ansistring; + a : ar; + n : smallint +) : smallint; + var + len, i, tmp : smallint; + begin + if s = '' then + exit(cal(a)); + libai3 := 0; + len := length(s); + i := 0; + while (i < n) and (i + libai3 < len) do + begin + inc(i); + tmp := libai3(copy(s, i + 1, len - i), putin(a, s, i), n + 1); + if tmp > libai3 then + libai3 := tmp + end + end; + +begin + assign(f, 'BAI3.INP'); + reset(f); + readln(f); + read(f, s0); + close(f); + setlength(a0, 1); + a0[0] := ''; + assign(f, 'BAI3.OUT'); + rewrite(f); + writeln(f, libai3(s0, a0, 1)); + close(f) +end. diff --git a/2ndary/12/TP-HN-2009/R2/R2.pdf b/2ndary/12/TP-HN-2009/R2/R2.pdf new file mode 100644 index 0000000..7af542a Binary files /dev/null and b/2ndary/12/TP-HN-2009/R2/R2.pdf differ -- cgit 1.4.1