 1. (1 ). ᫨  㣨 p y=f(x) i x=a  x=b.
H 㭪i (CURVE_LENGTH f a b eps),  eps - 娡 ᫥.



 2. (2 ). 㢠 i ஧⠭ 㦮   i. 
冷 i  i, 㦪 筮  冷 i.
ਪ, n=4: ((a b) c) d  (a (b c)) d  a ((b c) d)  (a b)(c d) 
                (a (b (c d)).



 3. (2 ). i쭥 ࠫ쭥 ᫮ N  󤨭 稭 ।⠢
  直 i i'󬭨 d[0], ... , d[s]  i
            N=d[s]*(s+1)!+d[s-1]*s! +...+d[1]*2!+d[0]           (*)
 㬮i,  0<=d[i]<=i+1, i=0,..,s,  d[s]<>0.
   s+1 ࠫ쭥 ᫮ d[0],  ..., d[s],  ࠫ쭥 K.   
i i ᫠ N, 瘟   䠪ii ⥬i (*) ᫠ 
d[0],  ..., d[s],  ᫮ K.



 4. ( , 5 i,   pii)

                                  Maximum Sum

  Background
  A problem that is simple to solve in one dimension is often much more
difficult to solve in more than one dimension. Consider satisfying a boolean
expression in conjunctive normal form in which each conjunct consists of
exactly 3 disjuncts. This problem (3-SAT) is NP-complete. The problem 2-SAT
is solved quite efficiently, however. In contrast, some problems belong to the
same complexity class regardless of the dimensionality of the problem.

(3-SATisfy -  3-)

  The Problem
  Given a 2-dimensional array of positive and negative integers, find the
sub-rectangle with the largest sum. The sum of a rectangle is the sum of all
the elements in that rectangle. In this problem the sub-rectangle with the
largest sum is referred to as the maximal sub-rectangle. A sub-rectangle is
any contiguous sub-array of size 1_1 or greater located within the whole array.
As an example, the maximal sub-rectangle of the array:

 0      -2      -7       0
 9       2      -6       2
-4       1      -4       1
-1       8       0      -2

is in the lower-left-hand corner:
  9     2
- 4     1
- 1     8

and has the sum of 15.

  Input and Output

  The input consists of an N*N array of integers. The input begins with a 
single positive integer N on a line by itself indicating the size of the 
square two dimensional array. This is followed by N2 integers separated by 
white-space (newlines and spaces). These N2 integers make up the array in 
row-major order (i.e., all numbers on the first row, left-to-right, then all 
numbers on the second row, left-to-right, etc.). N may be as large as 100. The 
numbers in the array will be in the range   [-127, 127].

  The output is the sum of the maximal sub-rectangle.

  Sample Input

4
0 -2 -7  0
9  2 -6  2
-4 1 -4  1
-1 8  0 -2

  Sample Output

15

  ⮢ i 䠩   home7.txt (p  䠩  ii 
 i).

  H⪠  brave-programmer: i  http://acm.gui.uva.es/problemset
pp  p' i.
  H   i p 108  pi ACM.



p.

p 1. ᫮  ᢮ i ।⠢ ( ᫠ 
 ॢ 10000 i ஧i). i ,  i
  15.

i:  i  9  ⪮i ⥬i ᫥  ii 
 9 㬨  ᫠ (i᭮, 堩  ᫮
     S = a[n]*10^n + a[n-1]*10^(n-1) + ... + a[1]*10 + a[0].

 S mod 9 = (a[n]*(10^n-1)+a[n] + a[n-1]*(10^(n-1)-1)+a[n-1] +
            + ... + a[1]*(10-1)+a[1] + a[0]) mod 9
      i쪨 10^k - 1 i  9,  i
          S mod 9 = (a[n] + ... +a[1] +a[0]) mod 9,
 ip).
  i筮  i  15  ⥬i ᫥  ᮬ 16 㤥
ii  15 㬨 i i⭠⪮  ᫠.
   ஧ i ᫮ ࠢ i  ࠤ, i 筮 
⢮p  i⭠⪮i , 室    i   15. 
 讪 0,   ᫮ i  15, i - i.


p 2.  ᫮  K-ii ⥬i ᫥
            a a   ...a   (K<=36).
             n n-1    0
 讪 i i   m.
  ᫠ K, n, m,  讪 i i  m ।⠢  ⪮i
⥬i ᫥.

i:  ⥬i ᫥  ᭮ K ᫮ ।⠢  i
              a[n]*K^n + a[n-1]*K^(n-1) + ... +a[0]*K^0.
   讪 i i   m (讪 i i a  b 稬
p a mod b):

   (a[n]*K^n +  a[n-1]*K^(n-1)  +  ...  +a[0]*K^0) mod m =

     n                     n                   
  = SUM a[i]*K^i mod m =  SUM a[i]* (K^i mod m) mod m =
    i=0                   i=0                  

  ⠭ pii   㯭:
  H堩 K^i mod m=t, ⮤i K^i =p*m+t, 

      (a[i]*K^i) mod m = (a[i] * (p*m+t)) mod m =
       = (a[i]* p*m) mod m + (a[i]*t) mod m =
       = (a[i] * (K^i mod m)) mod m,
   쮬  i쭨 ᥫ b[i] 
    n                   n
 ( SUM b[i] ) mod m =( SUM b[i]  mod m ) mod m.
   i=0                 i=0

  ii⨬ ⠪ 祢 ii
     K^i mod m =[(K^(i-1) mod m) * K] mod m,

  i쪨  K^(i-1) = p*m+t, 
              K^(i-1) mod m = t,
                K^i = p*m*K+t*K  K^i mod m = t*K mod m =
                = [(K^(i-1) mod m)*K] mod m.

   쮣  ( a[i] - K-ii  ᫠):
      s:=0;  t:=1;
      for i:=0 to n do
        begin
          s:=(s+a[i]*t) mod m;
          t:=t*K mod m;
        end;
   ii S i i祭 ஡  㤥 pi 
㪠 .


p 3.ip㢠 ii   i  ᫠ i.
ii pi  p'離 i   iii.

i:
  cnt:=0;               cnt -- i稫쭨   i i.
  while (i<>0) do       横  ii ࠧi, i
    begin               i쪮i   i. "" ࠩ
      i:=(i-1) and i;   ࠢ   i  ᫠.
      cnt:=cnt+1;       
    end;                ਪ:  110  = i
                                  101  = i-1
                                  ------------------
                                  100  = i and (i-1)


p 4. ii 011212201220200112...  ⠪: ᯮ
 0, i  㯭 i:  ᠭ ⨭ ਯ
ࠢ i i 0  1, 1  2, 2  0, ⮡
                0 -> 01 -> 0112 -> 01121220 ->...
   , 直   N, (0<=N<=3000000000) , 瘟 ᫮
  N- ii  ii.

i: H堩 a(k) - k- 童 ii. ﭥ ii, 
p  㯭 p:
                             a(0)=0;

                         a(0)...a(2**k-1)
p ਯ㢠  i ii ࠢ i  ii,
  쮬  童 ਯ ⨭ i  . 
p
                    0->01->0112->01121223->...
  ,  a(k)  㬠   i ।⠢i ᫠ k.
   ஢  ii.  a(0)=0  ࠢ. H堩
p饭 ࠢ     a(i), 0<=i<=2^(k-1)-1 (⮡  i 
ᥫ i, i ᪫  i i  k-1- i ஧i).
i  i pi l, 2^(k-1)<=l<2^k,   k- ஧i '
⪮ , i ⮬  a(l)=1+a(l-2^(k-1)).
  i쬥 a(i) mod 3, i p ᫮, 瘟   i- ii 
ii, ᠭ  㬮i i.  ⮣, 鮡  a(i), i,
 , ip㢠 ii   i  ᫠ i - 
. p 3.



ॢ

祭. ॢ    横i,  类 i ६ 
設,   ७ ॢ. 

 ⨯ ॢ 㤥   NIL (஦ ॢ),  
(祭 . (i ᨭ . ࠢ ᨭ)),  i  ࠢ ᨭ  ࠬ 
⨯ ॢ. ਪ, ॢ 瘟 ᪫  󤨭 , 㤥  
: (Element . (NIL . NIL)).

㭪i INSEL ⠢  n  ॢ tree  㯭 ࠢ:  
ॢ ஦,  ⢮ ॢ (n . (NIL . NIL)). I ⠢  
 i iॢ  n   祭 筮 設   ࠢ 
iॢ,  i. 㭪i INSL ⢮  ᯨ᪮  ॢ, 
設 类  i  ᯨ᪠. ॢ  稬, 
i쪨  室i  i ࠢ  ਬ i⮢ ᯨ᮪ 
i  箬 浪.

(DEFUN insel (n tree)
((NULL tree) (CONS n (CONS NIL NIL)))
((> n (CAR tree)) (cons (car tree) (cons (cadr tree) (insel n (cddr tree)))))
(cons (car tree) (cons (insel n (cadr tree)) (cddr tree)))  )

(DEFUN INSL (lst tree)
((NULL lst) tree)
(SETQ tree (insel (car lst) tree))
(INSL (CDR lst) tree)  )

㯭i i 㭪i  i ॢ: PUD (Print Up-Down) - i 
, PLR (Print Left-Right) - i i ࠢ.

(DEFUN PUD (tree)                           (DEFUN PLR (tree)
((NULL tree))                               	((NULL tree))
(PRIN1 (CAR tree)) (SPACES 3)        		(PLR (CADR tree))
(PUD (CADR tree))                            	(PRIN1 (CAR tree)) (SPACES 3)
(PUD (CDDR tree))  )                         (PLR (CDDR tree))  )

㭪i REVT (Reverse Tree)  ॢ:  ࠢ iॢ  i 
iॢ i .

(DEFUN REVT (tree)
((NULL tree) NIL)
(CONS (CAR tree) (CONS (REVT (CDDR tree)) (REVT (CADR tree))))  )

ਪ

$ (SETQ a (INSL '(5 1 7 3 9 2 4 8 10) NIL))	$ (SETQ b (REVT a))
$ (PLR a)					$ (PLR b)
1 2 3 4 5 7 8 9 10 T			        10 9 8 7 5 4 3 2 1

㭪i HEIGHT   ॢ. ⨬,   ஦쮣 
ॢ i 0.  ஦쮣 ॢ i ᨬ㬮i i 
⠬ i  ࠢ iॢ  . (HEIGHT a) = 4,  a  
 ।쮣 ਪ.

(DEFUN HEIGHT (tree)
((NULL tree) 0)
(MAX (ADD1 (HEIGHT (CADR tree))) (ADD1 (HEIGHT (CDDR tree))))  )



㭪i i

㭪i i  ॠi ii    
 ணࠬ㢠 i.

1. RPLACA <'1> <'2>. i㢠 i CAR- '1 
i  '2,  i '.  '1 - 
ᯨ᮪,  訩  ᯨ᪠ i  '2.  '1 - 
i୥ ॢ,   i ᨭ i  '2.  '1 - 
ᨬ (a  NIL),  ᨬ ਩ 祭 '2. 

$ (SETQ a '(a b c d))	$ (SETQ b '((1 . 2) . (3 . 4)))	$ (SETQ s 'd)
$ (RPLACA a '(11 12))	$ (RPLACA b 5)			$ (RPLACA s 'g)
((11 12) b c d)		(5 . (3 . 4))			Val(s)=d,Val(d) = g

2. RPLACD <'1> <'2>. i㢠 i CDR- '1 
i  '2,  i '. RPLACA  RPLACD 
 ᭮ 㭪iﬨ, i i i  ᯨi.   
।⠢ १ 㧠쭥 㭪i ᢮ SETF:

(RPLACA x y)	-  (SETF (CAR x) y)
(RPLACD x y)	-  (SETF (CDR x) y)

3. NSUBSTITUTE <><਩> <ᯨ᮪> <>. i  鮣 
i ᯨ. i  i  i  쮢 ii 
i,  直 ॢiઠ    i NIL.    
,   祭  = EQL.

$ (NSUBSTITUTE 1 3 '(4 5 6 (3 3 4 5) 3 4 1))
(4 5 6 (3 3 4 5) 1 4 1)

$ (NSUBSTITUTE 10 5 '(4 5 6 3 4 1) >)	$ (NSUBSTITUTE 10 5 '(4 5 6 3 4 1) <)
(10 5 6 10 10 10)			(4 5 10 3 4 1)

4. NSUBST <><਩> <ᯨ᮪> <>. 㭪i   i NSUBSTITUTE, 
 i  i ii ᯨ.

$ (NSUBST 1 3 '(4 5 6 (3 3 4 5) 3 4 1))
(4 5 6 (1 1 4 5) 1 4 1)

5. DELETE <> <ᯨ᮪> <>.  i ᯨ i ,  直 
 ॢiન  ⮬  i NIL.

$ (DELETE 3 '(1 2 3 4 3 2 1))
(1 2 4 2 1)

6. NREVERSE <ᯨ᮪> <'>.   ᯨ, 祯  '⮬.

$ (NREVERSE '(a b c d))	$ (NREVERSE '(1 2 3 (1 2 3) 4 5 6) '(1 2 3))
(d c b a)				(6 5 4 (1 2 3) 3 2 1 1 2 3)

7. NBUTLAST <ᯨ᮪> <n>.  n -   ⭥ i,  㭪i NBUTLAST 
 ᯨ᮪  n ⠭i i (i㢠 i n- , 
⮣  i ᯨ  NIL).  㣨 㬥  ,   
祭 n=1.

$ (NBUTLAST '(a b c d e))	$ (NBUTLAST '(a b c d e) 3)
(a b c d)			(a b)

8. NCONC <ᯨ᮪1> <ᯨ᮪2> ... <ᯨ᮪N>.  ᯨ᮪, 直 
᪫  i ᯨi - 㬥i   浪. i㢠 
ii ⠭i CDR-i ᯨi.    
(NCONC list list),  list - -直 ᯨ᮪,  १⮬ 㤥 
⭨ ᯨ᮪,  㤮 类 㤥 i祭.

$ (NCONC '(1 2) '(3 4) '(5 6 7)) 
(1 2 3 4 5 6 7)

9. SPLIT <ᯨ᮪>.  ᯨ᮪   ᯨ᪨ ।i. 祭 ᯨ 
   . 㭪i SPLIT    ᯨ.

$ (SETQ a '(1 2 3 4 5 6))		$ a
$ (SPLIT a)				(1 2 3)
(4 5 6)

10. SORT <ᯨ᮪> <>.   ᯨ  ᭮i .

$ (SORT '(2 5 3 4 1 6 8 9 7) >)
(9 8 7 6 5 4 3 2 1)




1.  ii i   ॢi.
2.  । 䬥筥 ᥫ, i 室  設 ॢ.
3.  㭪i 袨 㢠 QSORT.



iii

1. $ (DEFUN l (tree)	$ (DEFUN leaves (tree)
     (SETQ Counter1 0)	((AND (NULL (CADR tree)) (NULL (CDDR tree)))
     (leaves tree)		(INCQ Counter1))
     Counter1 )		(IF (NOT (NULL (CADR tree))) (leaves (CADR tree)))
			(IF (NOT (NULL (CDDR tree))) (leaves (CDDR tree))) ) 

2. $ (DEFUN count (tree)
      ((NULL tree) 0)
      (+ 1 (count (CADR tree)) (count (CDDR tree)))  )
   $ (DEFUN sm (tree)
      ((NULL tree) 0)
      (+ (CAR tree) (sm (CADR tree)) (sm (CDDR tree)))  )
   $ (DEFUN average (tree)
      (/ (sm tree) (count tree))  )

3. $ (DEFUN QSORT (lst)
         ((NULL (CDR lst)) lst)
         ((NULL (CDDR lst)) 
                ((< (CAR lst) (CADR lst)) lst)
                (CONS (CADR lst) (CONS (CAR lst) NIL)))
         (SETQ tmp1 (SPLIT lst))
         (APPL (QSORT tmp1) (QSORT lst))  )



  䠩

 祭  i ⮪  (CIS - Current Input Stream) 
 ᮫.

1.  ⠭   i ⮪ ⮢ 㭪i READ. i 
     (SETQ a (READ))  i   ᮫i ࠧ, 直 
   㤥 ⠭  ᢮ ii .  쮬  㤥  i쪠 
   'i,  ii  㤥 ᢮ 訩 '. ਪ,   
   : as bf gh,  i a ਩ 祭 as.     
   ᯨ᮪ (᪫ '),   i   㣫 㦪:
   (as df gh).

2. 㭪i (CLEAR-INPUT)    .  -类  
    NIL.

3. 㭪i (READ-LINE)    CIS   㤥 ⠭ ᨬ 
   室   冷 (<return>).  ᨬ, -i' 类 
   ᪫  i ⠭ ᨬi  i 㫨 ஧订i  i 
   浪, i <return>.

4. 㭪i (READ-CHAR)  㯭   CIS   .

5. 㭪i (UNREAD-CHAR)   CIS ⠭i ⠭ ᨬ.

6. 㭪i (LISTEN)  T  CIS  ஦i,  NIL   i諨  
   i 䠩.

7.  㭪i (OPEN-INPUT-FILE "<name>")  (CLOSE-INPUT-FILE "<name>") 
    ⮢  i   䠩 <name>  . 

8.  㭪i (OPEN-OUTPUT-FILE "<name>")  (CLOSE-OUTPUT-FILE "<name>") 
    ii iਢ  ਢ 䠩 <name>    iଠi.



ਪ

1. 㢠 ii i sym  䠩i name.

(DEFUN f (name sym)                       (SETQ a (READ))
(SETQ c 0)                                   (IF (EQL a sym) (INCQ c))  )
(OPEN-INPUT-FILE name)             	     (CLOSE-INPUT-FILE name)
(LOOP                                     c)
    ((NOT (LISTEN)))

2. 㢠 䠩  ୥ 浪,     ⮬.
(DEFUN rew (in out)                   (PUSH (READ) temp)   )
(OPEN-INPUT-FILE in)                  (LOOP
(OPEN-OUTPUT-FILE out)                ((EQL temp NIL))
(SETQ temp NIL)                            (WRITE (POP temp))
(LOOP                                      (SPACES 1)   )
   ((NOT (LISTEN)))                   (CLOSE-INPUT-FILE in)
                                           (CLOSE-OUTPUT-FILE out)   )



1.  㭪i (SRT <in> <out>), 猪  ⥪⮢ 䠩 <in>  
    i  䠩 <out>.
2.  㭪i (PRNUM2 num)  (PRNUM16 num), i ii  
   i ⪮i ᫠  i  i⭠⪮ ।⠢i.
3. 㢠   ᫮ n  ᨬ y ᯨ᮪ 
   (y yy yyy yyyy .... yyyyyyyy. ii i s  ⠭쮬 i 
   ᯨ i n.


iii

1. (DEFUN appl (lst1 lst2)
       ((NULL lst1) (append lst3 lst2))
       ((NULL lst2) (append lst3 lst1))
       ((STRING< (CAR lst1) (CAR lst2)) (CONS (CAR lst1) (appl (CDR lst1) lst2)))
       (CONS (CAR lst2) (APPL lst1 (CDR lst2)))  )

(DEFUN QSORT (lst)
       ((NULL (CDR lst)) lst)
       ((NULL (CDDR lst)) 
              ((STRING< (CAR lst) (CADR lst)) lst)
              (CONS (CADR lst) (CONS (CAR lst) NIL)))
       (SETQ tmp1 (SPLIT lst))
       (APPL (QSORT tmp1) (QSORT lst))  )

(DEFUN srt (in out)
(OPEN-INPUT-FILE in)
(OPEN-OUTPUT-FILE out)
(SETQ temp NIL)
(LOOP
   ((NOT (LISTEN)))
   (PUSH (READ) temp)   )
(setq temp1 (qsort temp))
(print temp1)
(CLOSE-INPUT-FILE in)
(CLOSE-OUTPUT-FILE out)   )

2. (DEFUN prnum2 (num)
       (SETQ res NIL)
       (LOOP
         ((= num 1))
         (SETQ tmp (DIVIDE num 2))
         (SETQ num (CAR tmp))
         (PUSH (CDR tmp) res)   )
       (PUSH 1 res)
       (PACK res)   )

(DEFUN prnum16 (num)
       (SETQ res NIL)
       (LOOP
          ((< num 16))
          (SETQ tmp (DIVIDE num 16))
          (SETQ num (CAR tmp) tmp (CDR tmp))
          (IF (>= tmp 10) (SETQ tmp (ASCII (+ (- 65 10) tmp))))
          (PUSH tmp res)  )
      (IF (>= num 10) (SETQ num (ASCII (+ (- 65 10) num))))
      (PUSH num res)
      (PACK res)  )

3. ; (m1 y 7 y) -> (y yy yyy yyyy yyyyy yyyyyy yyyyyyy)
(DEFUN m1 (sym n list)
       ((ZEROP n) NIL)
       (CONS (APPEND sym list) (m1 sym (- n 1) (PACK* sym list)))  )


