Advertisement
regergr

Untitled

Jan 1st, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. program Project1;
  2.  
  3. uses
  4. SysUtils;
  5.  
  6. const
  7. Q: QWord = 1000000000;
  8. type
  9. TLongNumber = array of QWord;
  10. type
  11. TBase = integer;
  12. function Invers_str(str: ansistring): ansistring;
  13. var
  14. newS: ansistring;
  15. i: integer;
  16. begin
  17. for i := length(str) downto 1 do
  18. begin
  19. newS += str[i];
  20. end;
  21. Result := newS;
  22. end;
  23. function invers_num(num:TLongNumber): TLongNumber;
  24.  
  25. var i, j:integer;
  26. begin
  27. j := high(num);
  28. Setlength(result, length(num));
  29. for i := 0 to high(num) do begin
  30. result[j] := num[i];
  31. j -= 1;
  32. end;
  33. end;
  34.  
  35. operator * (a, b: TLongNumber) ans: TLongNumber;
  36. var
  37. i, j: integer;
  38. s, buf, buf2: QWord;
  39. begin
  40. s := 0;
  41. begin
  42. SetLength(Result, high(a) + high(b) + 1);
  43. for i := 0 to high(Result) do
  44. begin
  45. Result[i] := 0;
  46. end;
  47. for i := 0 to high(b) do
  48. begin
  49. s := 0;
  50. for j := 0 to high(a) do
  51. begin
  52. buf := b[i] * a[j] + s + Result[i+j];
  53. s := buf div Q;
  54. Result[i+j]:=buf mod Q;
  55. end;
  56. if (s > 0) then
  57. begin
  58. if i = high(b) then
  59. SetLength(Result, Length(Result) + 1);
  60. Result[i + high(a) + 1] := s;
  61. end;
  62. end;
  63. end;
  64.  
  65. end;
  66. function Exp(num: TLongNumber; n: integer): TLongNumber;
  67. var
  68. x: TLongNumber;
  69. begin
  70. SetLength(x, 1);
  71. x[0] := 1;
  72. while (n <> 0) do
  73. begin
  74. if (n mod 2 = 1) then
  75. begin
  76. x := x * num;
  77. end;
  78. num := num * num;
  79. n := n div 2;
  80.  
  81. end;
  82. Result := x;
  83. end;
  84.  
  85.  
  86.  
  87.  
  88. function ToString(val: TLongNumber): ansistring;
  89. var i, j, count: integer;
  90. begin
  91. result := '';
  92. for i := high(val) downto 0 do begin
  93. // result += (Format(IntTostr(val[i]), ['%.9s']));
  94. if (length(IntToStr(val[i])) < 9) then begin
  95. count := 9 - length(IntToStr(val[i]));
  96. for j := 1 to count do Result += '0';
  97. end;
  98. Result += IntToStr(val[i]);
  99. end;
  100. i := 1;
  101. while result[i] = '0' do begin
  102. if (result = '0') then exit('0');
  103. Delete(result, i, 1);
  104. end;
  105.  
  106. end;
  107.  
  108.  
  109. function FromString(str: ansistring): TLongNumber;
  110. var n, i, j, len: integer;
  111. buf: string;
  112. begin
  113. len := (length(str) div 9) + 1;
  114. SetLength(Result, len);
  115. n := 0;
  116. i:= 1;
  117. str := invers_str(str);
  118. while i <= length(str) do begin
  119. buf := copy(str, i, 9);
  120. buf := invers_str(buf);
  121. i += 9;
  122. result[n] := StrToInt(buf);
  123. n += 1;
  124. end;
  125. //Result := invers_num(result);
  126. end;
  127.  
  128. function PowLong(s: ansistring; n: integer): ansistring;
  129. var
  130. num: TLongNumber;
  131. buf: TLongNumber;
  132. i: integer;
  133. begin
  134. if (n = 0) then
  135. exit('1');
  136. SetLength(buf, 1);
  137. buf[0] := 1;
  138. num := FromString(s);
  139. buf := Exp(num, n);
  140. Result := ToString(buf);
  141. end;
  142.  
  143. var
  144. s: ansistring;
  145. n: integer;
  146.  
  147. begin
  148.  
  149. Assign(input, 'input.txt');
  150. Assign(output, 'output.txt');
  151. reset(input);
  152. rewrite(output);
  153. readln(s);
  154. Read(n);
  155. Writeln(PowLong(s, n));
  156. // writeln(length(PowLong(s, n)));
  157.  
  158. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement