about summary refs log tree commit diff
path: root/others
diff options
context:
space:
mode:
authorRaphael McSinyx <vn.mcsinyx@gmail.com>2017-02-12 16:18:20 +0700
committerRaphael McSinyx <vn.mcsinyx@gmail.com>2017-02-12 16:18:20 +0700
commit1a8e1e68759611a66ae116771da5f376c95a3b9f (patch)
treee663fa6adb9264046a876e552ff2a455215c3a8b /others
parentdf9a0d140a7b7edef66f95aa13b5bb4488147f68 (diff)
downloadcp-1a8e1e68759611a66ae116771da5f376c95a3b9f.tar.gz
Update others/volume1
Diffstat (limited to 'others')
-rw-r--r--others/volume1/002.pas6
-rw-r--r--others/volume1/004.pas18
-rw-r--r--others/volume1/006.pas18
-rw-r--r--others/volume1/013.pas4
-rw-r--r--others/volume1/014.pas4
-rw-r--r--others/volume1/018.pas9
-rw-r--r--others/volume1/020.pas24
-rw-r--r--others/volume1/023.pas17
-rw-r--r--others/volume1/024.pas16
-rw-r--r--others/volume1/025.pas18
-rw-r--r--others/volume1/026.pas20
-rw-r--r--others/volume1/027.pas16
-rw-r--r--others/volume1/028.pas19
-rw-r--r--others/volume1/029.pas23
-rw-r--r--others/volume1/030.pas21
-rw-r--r--others/volume1/031.pas28
-rw-r--r--others/volume1/032.pas24
-rw-r--r--others/volume1/034.pas14
-rw-r--r--others/volume1/035.pas29
-rw-r--r--others/volume1/038.pas29
-rw-r--r--others/volume1/cmath.pas137
-rw-r--r--others/volume1/sortnfind.pas82
22 files changed, 521 insertions, 55 deletions
diff --git a/others/volume1/002.pas b/others/volume1/002.pas
index 7732052..a60a15c 100644
--- a/others/volume1/002.pas
+++ b/others/volume1/002.pas
@@ -1,4 +1,4 @@
-uses fibonacci;
+uses cmath;
 
 var
   m: int64;
@@ -13,9 +13,9 @@ begin
     end;
 
   for i := 3 to 88 do
-    if fibonacci93[i] > m then
+    if fibonacci[i] > m then
       begin
-        writeln(i - 1, ' ', fibonacci93[i - 1]);
+        writeln(i - 1, ' ', fibonacci[i - 1]);
         exit
       end
 end.
diff --git a/others/volume1/004.pas b/others/volume1/004.pas
index caf5142..9ade431 100644
--- a/others/volume1/004.pas
+++ b/others/volume1/004.pas
@@ -1,7 +1,8 @@
+uses cmath;
+
 var
   prime: array [2..1000000] of boolean;
   i, j, n, k: longint;
-  b: boolean = true;
 
 begin
   for i := 2 to 1000000 do
@@ -12,20 +13,7 @@ begin
         prime[i * j] := false;
 
   readln(n, k);
-  if n < 2 then
-    writeln('FALSE')
-  else if n <= 1000000 then
-    writeln(prime[n])
-  else
-    begin
-      for i := 2 to trunc(sqrt(n)) do
-        if n mod i = 0 then
-        begin
-          b := false;
-          break
-        end;
-      writeln(b)
-    end;
+  writeln(isprime(n));
   for i := 2 to k do
     if prime[i] then
       write(i, ' ');
diff --git a/others/volume1/006.pas b/others/volume1/006.pas
index 989acba..878f077 100644
--- a/others/volume1/006.pas
+++ b/others/volume1/006.pas
@@ -1,23 +1,9 @@
+uses cmath;
+
 var
   a, b, g: int64;
   c, d: longint;
 
-
-function gcd(x, y: int64): int64;
-  var
-    z: longint;
-
-  begin
-    while y > 0 do
-      begin
-        z := y;
-        y := x mod y;
-        x := z
-      end;
-    gcd := x
-  end;
-
-
 begin
   readln(a, b, c, d);
   a := a * d + b * c;
diff --git a/others/volume1/013.pas b/others/volume1/013.pas
index 4884a20..d257945 100644
--- a/others/volume1/013.pas
+++ b/others/volume1/013.pas
@@ -1,9 +1,9 @@
-uses fibonacci;
+uses cmath;
 
 var
   n: shortint;
 
 begin
   readln(n);
-  writeln(fibonacci93[n + 1])
+  writeln(fibonacci[n + 1])
 end.
diff --git a/others/volume1/014.pas b/others/volume1/014.pas
index d3d19c9..3407e70 100644
--- a/others/volume1/014.pas
+++ b/others/volume1/014.pas
@@ -1,9 +1,9 @@
-uses fibonacci;
+uses cmath;
 
 var
   n: shortint;
 
 begin
   readln(n);
-  writeln(fibonacci93[n + 2])
+  writeln(fibonacci[n + 2])
 end.
diff --git a/others/volume1/018.pas b/others/volume1/018.pas
index 05fd7e9..053095c 100644
--- a/others/volume1/018.pas
+++ b/others/volume1/018.pas
@@ -1,11 +1,12 @@
+uses cmath;
+
 var
   n: int64;
 
 begin
   readln(n);
-  if (n < 0) or
-     (sqr(trunc(sqrt(n))) <> n) then
-    writeln('NO')
-  else
+  if issquare(n) then
     writeln('YES')
+  else
+    writeln('NO')
 end.
diff --git a/others/volume1/020.pas b/others/volume1/020.pas
index 4d11bd2..f37db27 100644
--- a/others/volume1/020.pas
+++ b/others/volume1/020.pas
@@ -9,24 +9,22 @@ begin
     read(a[l]);
   a[n] := 0;
   max := 0;
-  l := 0;
-  h := 0;
-
   l0 := 0;
-  while l0 < n do
-    for h0 := l0 to n - 1 do
-      if a[h0] * a[h0 + 1] >= 0 then
+  h0 := 0;
+  l := 0;
+  while l < n do
+    for h := l to n - 1 do
+      if a[h] * a[h + 1] >= 0 then
         begin
-          if max < h0 - l0 then
+          if max < h - l then
             begin
-              max := h0 - l0;
-              l := l0;
-              h := h0
+              max := h - l;
+              l0 := l;
+              h0 := h
             end;
-          l0 := h0 + 1;
+          l := h + 1;
           break
         end;
 
-  writeln(l + 1, ' ', h + 1)
-end.
+  writeln(l0 + 1, ' ', h0 + 1)
 end.
diff --git a/others/volume1/023.pas b/others/volume1/023.pas
new file mode 100644
index 0000000..6092889
--- /dev/null
+++ b/others/volume1/023.pas
@@ -0,0 +1,17 @@
+uses strutils, cmath;
+
+var
+  n, u: int64;
+  s: string;
+  code: word;
+
+begin
+  readln(n);
+  str(n, s);
+  val(reversestring(s), u, code);
+  if isprime(n) and
+     isprime(u) then
+    writeln('YES')
+  else
+    writeln('NO')
+end.
diff --git a/others/volume1/024.pas b/others/volume1/024.pas
new file mode 100644
index 0000000..affcbaa
--- /dev/null
+++ b/others/volume1/024.pas
@@ -0,0 +1,16 @@
+uses cmath;
+
+var
+  n: int64;
+  b: boolean;
+
+begin
+  readln(n);
+  b := isprime(n);
+  while b and (n > 9) do
+    begin
+      n := n div 10;
+      b := b and isprime(n)
+    end;
+  writeln(b)
+end.
diff --git a/others/volume1/025.pas b/others/volume1/025.pas
new file mode 100644
index 0000000..ff9f2c6
--- /dev/null
+++ b/others/volume1/025.pas
@@ -0,0 +1,18 @@
+var
+  x0, y0, x1, y1, x2, y2: int64;
+
+begin
+  read(x0, y0, x1, y1, x2, y2);
+  if x1 = x2 then
+    write(x0, ' ')
+  else if x2 = x0 then
+    write(x1, ' ')
+  else
+    write(x2, ' ');
+  if y1 = y2 then
+    writeln(y0)
+  else if y2 = y0 then
+    writeln(y1)
+  else
+    writeln(y2)
+end.
diff --git a/others/volume1/026.pas b/others/volume1/026.pas
new file mode 100644
index 0000000..7b76472
--- /dev/null
+++ b/others/volume1/026.pas
@@ -0,0 +1,20 @@
+const
+  enum: array[0..2] of string = ('none', 'one', 'both');
+
+var
+  (* 1 ngày = 1440 phút *)
+  a, b: array[0..1] of int16;
+  m: array[0..2] of int16;
+  i, j, dogs: int8;
+
+begin
+  read(a[0], b[0], a[1], b[1], m[0], m[1], m[2]);
+  for i := 0 to 2 do
+    begin
+      dogs := 0;
+      for j := 0 to 1 do
+        if (m[i] - 1) mod (a[j] + b[j]) < a[j] then
+          inc(dogs);
+      writeln(enum[dogs])
+    end
+end.
diff --git a/others/volume1/027.pas b/others/volume1/027.pas
new file mode 100644
index 0000000..0b285bd
--- /dev/null
+++ b/others/volume1/027.pas
@@ -0,0 +1,16 @@
+uses cmath;
+
+var
+  n: int64;
+  m: int32;
+
+begin
+  readln(n);
+  m := trunc(sqrt(n));
+  if issquare(n) then
+    writeln(m, ' ', m)
+  else if n > m * (m + 1) then
+    writeln(m + 1, ' ', m + 1)
+  else
+    writeln(m, ' ', m + 1)
+end.
diff --git a/others/volume1/028.pas b/others/volume1/028.pas
new file mode 100644
index 0000000..f135a67
--- /dev/null
+++ b/others/volume1/028.pas
@@ -0,0 +1,19 @@
+var
+  a, i: int16;
+  b: array[0..9999] of boolean;
+
+begin
+  for i := 0 to 9999 do
+    b[i] := false;
+  readln(a);
+  for i := 0 to 9999 do
+    begin
+      b[a] := true;
+      a := sqr(a) div 100 mod 10000
+    end;
+  a := 0;
+  for i := 0 to 9999 do
+    if b[i] then
+      inc(a);
+  writeln(a)
+end.
diff --git a/others/volume1/029.pas b/others/volume1/029.pas
new file mode 100644
index 0000000..255331f
--- /dev/null
+++ b/others/volume1/029.pas
@@ -0,0 +1,23 @@
+uses sortnfind;
+
+var
+  n, i: int16;
+  p: int64;
+  a, b: array of int64;
+
+begin
+  readln(n, p);
+  setlength(a, n);
+  setlength(b, n);
+  for i := 0 to n - 1 do
+    begin
+      read(a[i]);
+      b[i] := a[i]
+    end;
+  qsort(a);
+  p := a[p - 1];
+  for i := 0 to n - 1 do
+    if b[i] = p then
+      break;
+  writeln(p, ' ', i + 1)
+end.
diff --git a/others/volume1/030.pas b/others/volume1/030.pas
new file mode 100644
index 0000000..95490f2
--- /dev/null
+++ b/others/volume1/030.pas
@@ -0,0 +1,21 @@
+var
+  s: ansistring;
+
+function ispalindrome(s: ansistring): boolean;
+  var
+    n, i: int32;
+  begin
+    n := length(s);
+    for i := 1 to n do
+      if s[i] <> s[n - i + 1] then
+        exit(false);
+      ispalindrome := true
+  end;
+
+begin
+  readln(s);
+  if ispalindrome(s) then
+    writeln('YES')
+  else
+    writeln('NO')
+end.
diff --git a/others/volume1/031.pas b/others/volume1/031.pas
new file mode 100644
index 0000000..d87ad88
--- /dev/null
+++ b/others/volume1/031.pas
@@ -0,0 +1,28 @@
+var
+  n, m, i, j, k, l: int16;
+  a: array of int16;
+
+begin
+  readln(n, m);
+  setlength(a, m);
+  for i := 0 to m - 1 do
+    a[i] := 0;
+  for i := 1 to n do
+    begin
+      read(k);
+      for j := 1 to k do
+        begin
+          read(l);
+          inc(a[l - 1])
+        end
+    end;
+  k := 0;
+  for i := 0 to m - 1 do
+    if a[i] = n then
+      inc(k);
+  l := 0;
+  for i := 0 to m - 1 do
+    if a[i] = 0 then
+      inc(l);
+  writeln(k, ' ', l)
+end.
diff --git a/others/volume1/032.pas b/others/volume1/032.pas
new file mode 100644
index 0000000..7813997
--- /dev/null
+++ b/others/volume1/032.pas
@@ -0,0 +1,24 @@
+uses sortnfind;
+
+var
+  n, i: int16;
+  a, b: array of int64;
+
+begin
+  readln(n);
+  setlength(a, n);
+  for i := 0 to n - 1 do
+    read(a[i]);
+  qsort(a);
+  setlength(b, n);
+  for i := 0 to n - 1 do
+    read(b[i]);
+  qsort(b);
+  for i := 0 to n - 1 do
+    if a[i] <> b[i] then
+      begin
+        writeln('NO');
+        exit
+      end;
+  writeln('YES')
+end.
diff --git a/others/volume1/034.pas b/others/volume1/034.pas
new file mode 100644
index 0000000..1e34793
--- /dev/null
+++ b/others/volume1/034.pas
@@ -0,0 +1,14 @@
+uses math, sortnfind;
+
+var
+  n, i: int16;
+  a: array of int64;
+
+begin
+  readln(n);
+  setlength(a, n);
+  for i := 0 to n - 1 do
+    read(a[i]);
+  qsort(a);
+  writeln(max(a[0] * a[1], a[n - 2] * a[n - 1]))
+end.
diff --git a/others/volume1/035.pas b/others/volume1/035.pas
new file mode 100644
index 0000000..ccf9560
--- /dev/null
+++ b/others/volume1/035.pas
@@ -0,0 +1,29 @@
+var
+  n, a: int16;
+  b: array of boolean;
+
+function all(b: array of boolean): boolean;
+  var
+    i: int16;
+  begin
+    for i := 0 to length(b) - 1 do
+      if not b[i] then
+        exit(false);
+    all := true
+  end;
+
+begin
+  readln(n);
+  setlength(b, n);
+  for n := n - 1 downto 0 do
+    b[n] := false;
+  for n := 1 to length(b) do
+    begin
+      read(a);
+      if (a <= 0) or
+         (a > length(b)) then
+        continue;
+      b[a - 1] := true
+    end;
+  writeln(all(b))
+end.
diff --git a/others/volume1/038.pas b/others/volume1/038.pas
new file mode 100644
index 0000000..a89142a
--- /dev/null
+++ b/others/volume1/038.pas
@@ -0,0 +1,29 @@
+var
+  n, l, h, l0, h0, len: int16;
+  a: array of int32;
+
+begin
+  readln(n);
+  setlength(a, n);
+  for l := 0 to n - 1 do
+    read(a[l]);
+  len := 0;
+  l0 := 0;
+  h0 := 0;
+  l := 0;
+  while l < n do
+    for h := l to n - 1 do
+      if (h = n - 1) or
+         ((a[h] mod a[h + 1]) * (a[h + 1] mod a[h]) > 0) then
+        begin
+          if len < h - l then
+            begin
+              len := h - l;
+              l0 := l;
+              h0 := h
+            end;
+          l := h + 1;
+          break
+        end;
+  writeln(l0 + 1, ' ', h0 + 1)
+end.
diff --git a/others/volume1/cmath.pas b/others/volume1/cmath.pas
new file mode 100644
index 0000000..9f4f1f5
--- /dev/null
+++ b/others/volume1/cmath.pas
@@ -0,0 +1,137 @@
+(* Common mathematical functions and data *)
+unit cmath;
+
+interface
+
+  const
+    fibonacci: array[1..93] of uint64 = (
+      1,
+      1,
+      2,
+      3,
+      5,
+      8,
+      13,
+      21,
+      34,
+      55,
+      89,
+      144,
+      233,
+      377,
+      610,
+      987,
+      1597,
+      2584,
+      4181,
+      6765,
+      10946,
+      17711,
+      28657,
+      46368,
+      75025,
+      121393,
+      196418,
+      317811,
+      514229,
+      832040,
+      1346269,
+      2178309,
+      3524578,
+      5702887,
+      9227465,
+      14930352,
+      24157817,
+      39088169,
+      63245986,
+      102334155,
+      165580141,
+      267914296,
+      433494437,
+      701408733,
+      1134903170,
+      1836311903,
+      2971215073,
+      4807526976,
+      7778742049,
+      12586269025,
+      20365011074,
+      32951280099,
+      53316291173,
+      86267571272,
+      139583862445,
+      225851433717,
+      365435296162,
+      591286729879,
+      956722026041,
+      1548008755920,
+      2504730781961,
+      4052739537881,
+      6557470319842,
+      10610209857723,
+      17167680177565,
+      27777890035288,
+      44945570212853,
+      72723460248141,
+      117669030460994,
+      190392490709135,
+      308061521170129,
+      498454011879264,
+      806515533049393,
+      1304969544928657,
+      2111485077978050,
+      3416454622906707,
+      5527939700884757,
+      8944394323791464,
+      14472334024676221,
+      23416728348467685,
+      37889062373143906,
+      61305790721611591,
+      99194853094755497,
+      160500643816367088,
+      259695496911122585,
+      420196140727489673,
+      679891637638612258,
+      1100087778366101931,
+      1779979416004714189,
+      2880067194370816120,
+      4660046610375530309,
+      7540113804746346429,
+      12200160415121876738
+    );
+
+  function gcd(x, y: int64): int64;
+  function isprime(x: int64): boolean;
+  function issquare(x: int64): boolean;
+
+implementation
+
+  function gcd(x, y: int64): int64;
+    var z: int64;
+    begin
+      while y > 0 do
+        begin
+          z := y;
+          y := x mod y;
+          x := z
+        end;
+      gcd := x
+    end;
+
+  function isprime(x: int64): boolean;
+    var i: int32;
+    begin
+      if x < 2 then
+        exit(false);
+      for i := 2 to trunc(sqrt(x)) do
+        if x mod i = 0 then
+          exit(false);
+      isprime := true
+    end;
+
+  function issquare(x: int64): boolean;
+    begin
+      issquare := (x >= 0) and (sqr(trunc(sqrt(x))) = x)
+    end;
+
+end.
diff --git a/others/volume1/sortnfind.pas b/others/volume1/sortnfind.pas
new file mode 100644
index 0000000..a65cde0
--- /dev/null
+++ b/others/volume1/sortnfind.pas
@@ -0,0 +1,82 @@
+unit sortnfind;
+
+interface
+
+  type
+    intar = array of int64;
+
+  procedure qsort(var a : intar);
+
+  function binin(
+    a: intar;
+    x: int64
+  ): boolean;
+
+implementation
+
+  procedure qsort(var a : intar);
+    procedure sort(l, r: integer);
+      var
+        i, j, x, y: integer;
+
+      begin
+        i := l;
+        j := r;
+        x := a[(l + r) div 2];
+
+        repeat
+          while a[i] < x do
+            inc(i);
+
+          while x < a[j] do
+            dec(j);
+
+          if i <= j then
+            begin
+              y := a[i];
+              a[i] := a[j];
+              a[j] := y;
+              inc(i);
+              j := j - 1
+            end
+        until i > j;
+
+        if l < j then
+          sort(l, j);
+
+        if i < r then
+          sort(i, r)
+      end;
+
+    begin
+      sort(0, length(a) - 1)
+    end;
+
+
+  function binin(
+    a: intar;
+    x: int64
+  ): boolean;
+
+    var
+      l, h, mid: uint64;
+
+    begin
+      l := 0;
+      h := length(a) - 1;
+
+      while l <= h do
+        begin
+          mid := (l + h) div 2;
+          if x = a[mid] then
+            exit(true)
+          else if x < a[mid] then
+            h := mid - 1
+          else
+            l := mid + 1
+        end;
+
+      binin := false
+    end;
+
+end.