<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>https://acm.khpnets.info/w39/index.php?action=history&amp;feed=atom&amp;title=%D0%A1%D0%BB%D0%BE%D0%B6%D0%BD%D0%B0%D1%8F_%D0%B4%D0%BB%D0%B8%D0%BD%D0%BD%D0%B0%D1%8F_%D0%B0%D1%80%D0%B8%D1%84%D0%BC%D0%B5%D1%82%D0%B8%D0%BA%D0%B0</id>
	<title>Сложная длинная арифметика - История изменений</title>
	<link rel="self" type="application/atom+xml" href="https://acm.khpnets.info/w39/index.php?action=history&amp;feed=atom&amp;title=%D0%A1%D0%BB%D0%BE%D0%B6%D0%BD%D0%B0%D1%8F_%D0%B4%D0%BB%D0%B8%D0%BD%D0%BD%D0%B0%D1%8F_%D0%B0%D1%80%D0%B8%D1%84%D0%BC%D0%B5%D1%82%D0%B8%D0%BA%D0%B0"/>
	<link rel="alternate" type="text/html" href="https://acm.khpnets.info/w39/index.php?title=%D0%A1%D0%BB%D0%BE%D0%B6%D0%BD%D0%B0%D1%8F_%D0%B4%D0%BB%D0%B8%D0%BD%D0%BD%D0%B0%D1%8F_%D0%B0%D1%80%D0%B8%D1%84%D0%BC%D0%B5%D1%82%D0%B8%D0%BA%D0%B0&amp;action=history"/>
	<updated>2026-05-13T12:15:47Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://acm.khpnets.info/w39/index.php?title=%D0%A1%D0%BB%D0%BE%D0%B6%D0%BD%D0%B0%D1%8F_%D0%B4%D0%BB%D0%B8%D0%BD%D0%BD%D0%B0%D1%8F_%D0%B0%D1%80%D0%B8%D1%84%D0%BC%D0%B5%D1%82%D0%B8%D0%BA%D0%B0&amp;diff=2312&amp;oldid=prev</id>
		<title>Ctrlalt: Новая страница: «== Более эффективные длинные целые ==   class PrintHelper {      int d[1000], size;  public:      PrintHelper() {          d[0] = 0;  …»</title>
		<link rel="alternate" type="text/html" href="https://acm.khpnets.info/w39/index.php?title=%D0%A1%D0%BB%D0%BE%D0%B6%D0%BD%D0%B0%D1%8F_%D0%B4%D0%BB%D0%B8%D0%BD%D0%BD%D0%B0%D1%8F_%D0%B0%D1%80%D0%B8%D1%84%D0%BC%D0%B5%D1%82%D0%B8%D0%BA%D0%B0&amp;diff=2312&amp;oldid=prev"/>
		<updated>2018-07-26T17:40:41Z</updated>

		<summary type="html">&lt;p&gt;Новая страница: «== Более эффективные длинные целые ==   class PrintHelper {      int d[1000], size;  public:      PrintHelper() {          d[0] = 0;  …»&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Более эффективные длинные целые ==&lt;br /&gt;
&lt;br /&gt;
 class PrintHelper {&lt;br /&gt;
     int d[1000], size;&lt;br /&gt;
 public:&lt;br /&gt;
     PrintHelper() {&lt;br /&gt;
         d[0] = 0;&lt;br /&gt;
         size = 1;&lt;br /&gt;
     }&lt;br /&gt;
     void add1() {&lt;br /&gt;
         d[0]++;&lt;br /&gt;
     }&lt;br /&gt;
     void mul2() {&lt;br /&gt;
         int carry = 0;&lt;br /&gt;
         for (int i = 0; i &amp;lt; size; i++) {&lt;br /&gt;
             int x = d[i] * 2 + carry;&lt;br /&gt;
             d[i] = x % 10;&lt;br /&gt;
             carry = x / 10;&lt;br /&gt;
         }&lt;br /&gt;
         if (carry)&lt;br /&gt;
             d[size++] = 1;&lt;br /&gt;
     }&lt;br /&gt;
     void print() const {&lt;br /&gt;
         for (int i = size - 1; i &amp;gt;= 0; i--)&lt;br /&gt;
             printf(&amp;quot;%d&amp;quot;, d[i]);&lt;br /&gt;
         printf(&amp;quot;\n&amp;quot;);&lt;br /&gt;
     }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 class BigInteger {&lt;br /&gt;
     static const int MAX_SIZE = 100;&lt;br /&gt;
     static const unsigned int MASK = ~(1 &amp;lt;&amp;lt; 31);&lt;br /&gt;
     unsigned int d[MAX_SIZE];&lt;br /&gt;
     int size;&lt;br /&gt;
 public:&lt;br /&gt;
     BigInteger(unsigned long long val = 0) {&lt;br /&gt;
         for (int i = 0; i &amp;lt; MAX_SIZE; i++)&lt;br /&gt;
             d[i] = 0;&lt;br /&gt;
         size = 0;&lt;br /&gt;
         if (val == 0)&lt;br /&gt;
             d[size++] = 0;&lt;br /&gt;
         while (val &amp;gt; 0) {&lt;br /&gt;
             d[size++] = val &amp;amp; MASK;&lt;br /&gt;
             val &amp;gt;&amp;gt;= 31;&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     void print() const {&lt;br /&gt;
         PrintHelper p;&lt;br /&gt;
         for (int i = size - 1; i &amp;gt;= 0; i--) {&lt;br /&gt;
             for (int bit = 30; bit &amp;gt;= 0; bit--) {&lt;br /&gt;
                 p.mul2();&lt;br /&gt;
                 if ((d[i] &amp;gt;&amp;gt; bit) &amp;amp; 1)&lt;br /&gt;
                     p.add1();&lt;br /&gt;
             }&lt;br /&gt;
         }&lt;br /&gt;
         p.print();&lt;br /&gt;
     }&lt;br /&gt;
     int cmp(const BigInteger &amp;amp;that) const {&lt;br /&gt;
         if (size != that.size)&lt;br /&gt;
             return size &amp;lt; that.size ? -1 : 1;&lt;br /&gt;
         for (int i = size - 1; i &amp;gt;= 0; i--) {&lt;br /&gt;
             if (d[i] != that.d[i])&lt;br /&gt;
                 return d[i] &amp;lt; that.d[i] ? -1 : 1;&lt;br /&gt;
         }&lt;br /&gt;
         return 0;&lt;br /&gt;
     }&lt;br /&gt;
     bool operator &amp;lt; (const BigInteger &amp;amp;that) const {&lt;br /&gt;
         return cmp(that) &amp;lt; 0;&lt;br /&gt;
     }&lt;br /&gt;
     bool operator == (const BigInteger &amp;amp;that) const {&lt;br /&gt;
         return cmp(that) == 0;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger &amp;amp;operator += (const BigInteger &amp;amp;that) {&lt;br /&gt;
         unsigned int carry = 0;&lt;br /&gt;
         for (int i = 0; i &amp;lt; max(size, that.size) || carry != 0; i++) {&lt;br /&gt;
             unsigned long long x = 0ULL + d[i] + that.d[i] + carry;&lt;br /&gt;
             d[i] = x &amp;amp; MASK;&lt;br /&gt;
             carry = x &amp;gt;&amp;gt; 31;&lt;br /&gt;
         }&lt;br /&gt;
         size = max(size, that.size) + 1;&lt;br /&gt;
         while (size &amp;gt; 1 &amp;amp;&amp;amp; d[size - 1] == 0)&lt;br /&gt;
             size--;&lt;br /&gt;
         return *this;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger operator + (const BigInteger &amp;amp;that) const {&lt;br /&gt;
         BigInteger res = *this;&lt;br /&gt;
         return res += that;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger &amp;amp;operator -= (const BigInteger &amp;amp;that) {&lt;br /&gt;
         int carry = 0;&lt;br /&gt;
         for (int i = 0; i &amp;lt; size || carry != 0; i++) {&lt;br /&gt;
             long long x = 0LL + d[i] - that.d[i] + carry;&lt;br /&gt;
             d[i] = (x + (1ULL &amp;lt;&amp;lt; 31)) &amp;amp; MASK;&lt;br /&gt;
             carry = x &amp;lt; 0 ? -1 : 0;&lt;br /&gt;
         }&lt;br /&gt;
         while (size &amp;gt; 1 &amp;amp;&amp;amp; d[size - 1] == 0)&lt;br /&gt;
             size--;&lt;br /&gt;
         return *this;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger operator - (const BigInteger &amp;amp;that) const {&lt;br /&gt;
         BigInteger res = *this;&lt;br /&gt;
         return res -= that;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger &amp;amp;operator *= (const BigInteger &amp;amp;that) {&lt;br /&gt;
         unsigned int rd[MAX_SIZE] = {};&lt;br /&gt;
         for (int i = 0; i &amp;lt; size; i++) {&lt;br /&gt;
             unsigned int carry = 0;&lt;br /&gt;
             for (int j = 0; j &amp;lt; that.size || carry != 0; j++) {&lt;br /&gt;
                 unsigned long long x = rd[i + j] + 1ULL * d[i] * that.d[j] + carry;&lt;br /&gt;
                 rd[i + j] = x &amp;amp; MASK;&lt;br /&gt;
                 carry = x &amp;gt;&amp;gt; 31;&lt;br /&gt;
             }&lt;br /&gt;
         }&lt;br /&gt;
         for (int i = 0; i &amp;lt; MAX_SIZE; i++)&lt;br /&gt;
             d[i] = rd[i];&lt;br /&gt;
         size = size + that.size;&lt;br /&gt;
         while (size &amp;gt; 1 &amp;amp;&amp;amp; d[size - 1] == 0)&lt;br /&gt;
             size--;&lt;br /&gt;
         return *this;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger operator * (const BigInteger &amp;amp;that) const {&lt;br /&gt;
         BigInteger res = *this;&lt;br /&gt;
         return res *= that;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger &amp;amp;operator *= (unsigned int that) {&lt;br /&gt;
         unsigned int carry = 0;&lt;br /&gt;
         for (int i = 0; i &amp;lt; size || carry != 0; i++) {&lt;br /&gt;
             unsigned long long x = 1ULL * d[i] * that + carry;&lt;br /&gt;
             d[i] = x &amp;amp; MASK;&lt;br /&gt;
             carry = x &amp;gt;&amp;gt; 31;&lt;br /&gt;
         }&lt;br /&gt;
         size = size + 1;&lt;br /&gt;
         while (size &amp;gt; 1 &amp;amp;&amp;amp; d[size - 1] == 0)&lt;br /&gt;
             size--;&lt;br /&gt;
         return *this;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger operator * (unsigned int that) const {&lt;br /&gt;
         BigInteger res = *this;&lt;br /&gt;
         return res *= that;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger &amp;amp;operator /= (const BigInteger &amp;amp;that) {&lt;br /&gt;
         BigInteger carry;&lt;br /&gt;
         for (int i = size - 1; i &amp;gt;= 0; i--) {&lt;br /&gt;
             carry *= (1ULL &amp;lt;&amp;lt; 31);&lt;br /&gt;
             carry.d[0] = d[i];&lt;br /&gt;
             unsigned int l = 0, r = (1 &amp;lt;&amp;lt; 31), m;&lt;br /&gt;
             while (l + 1 &amp;lt; r) {&lt;br /&gt;
                 m = l + (r - l) / 2;&lt;br /&gt;
                 if ((that * m).cmp(carry) &amp;lt;= 0)&lt;br /&gt;
                     l = m;&lt;br /&gt;
                 else&lt;br /&gt;
                     r = m;&lt;br /&gt;
             }&lt;br /&gt;
             d[i] = l;&lt;br /&gt;
             carry = carry - that * d[i];&lt;br /&gt;
         }&lt;br /&gt;
         while (size &amp;gt; 1 &amp;amp;&amp;amp; d[size - 1] == 0)&lt;br /&gt;
             size--;&lt;br /&gt;
         return *this;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger operator / (const BigInteger &amp;amp;that) const {&lt;br /&gt;
         BigInteger res = *this;&lt;br /&gt;
         return res /= that;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger operator % (const BigInteger &amp;amp;that) const {&lt;br /&gt;
         BigInteger carry;&lt;br /&gt;
         for (int i = size - 1; i &amp;gt;= 0; i--) {&lt;br /&gt;
             carry *= (1ULL &amp;lt;&amp;lt; 31);&lt;br /&gt;
             carry.d[0] = d[i];&lt;br /&gt;
             unsigned int l = 0, r = (1 &amp;lt;&amp;lt; 31), m;&lt;br /&gt;
             while (l + 1 &amp;lt; r) {&lt;br /&gt;
                 m = l + (r - l) / 2;&lt;br /&gt;
                 if ((that * m).cmp(carry) &amp;lt;= 0)&lt;br /&gt;
                     l = m;&lt;br /&gt;
                 else&lt;br /&gt;
                     r = m;&lt;br /&gt;
             }&lt;br /&gt;
             unsigned int digit = l;&lt;br /&gt;
             carry = carry - that * digit;&lt;br /&gt;
         }&lt;br /&gt;
         return carry;&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger gcd(const BigInteger &amp;amp;that) const {&lt;br /&gt;
         if (that == 0)&lt;br /&gt;
             return *this;&lt;br /&gt;
         return that.gcd(*this % that);&lt;br /&gt;
     }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
== Длинные целые со знаком ==&lt;br /&gt;
&lt;br /&gt;
 class BigSignedInteger {&lt;br /&gt;
     BigInteger a;&lt;br /&gt;
     int sgn;&lt;br /&gt;
     BigSignedInteger(const BigInteger &amp;amp;a, int sign) : a(a), sgn(sign) {}&lt;br /&gt;
 public:&lt;br /&gt;
     BigSignedInteger(long long val = 0) {&lt;br /&gt;
         a = ::abs(val);&lt;br /&gt;
         sgn = val ? val &amp;gt; 0 ? 1 : -1 : 0;&lt;br /&gt;
     }&lt;br /&gt;
     BigSignedInteger(const BigInteger &amp;amp;val) {&lt;br /&gt;
         a = val;&lt;br /&gt;
         sgn = val.cmp(0);&lt;br /&gt;
     }&lt;br /&gt;
     void print() const {&lt;br /&gt;
         if (sgn == -1)&lt;br /&gt;
             printf(&amp;quot;-&amp;quot;);&lt;br /&gt;
         a.print();&lt;br /&gt;
     }&lt;br /&gt;
     BigInteger abs() const {&lt;br /&gt;
         return a;&lt;br /&gt;
     }&lt;br /&gt;
     int sign() const {&lt;br /&gt;
         return sgn;&lt;br /&gt;
     }&lt;br /&gt;
     int cmp(const BigSignedInteger &amp;amp;that) const {&lt;br /&gt;
         if (sgn != that.sgn)&lt;br /&gt;
             return sgn &amp;lt; that.sgn ? -1 : 1;&lt;br /&gt;
         return sgn * a.cmp(that.a);&lt;br /&gt;
     }&lt;br /&gt;
     bool operator &amp;lt; (const BigSignedInteger &amp;amp;that) const {&lt;br /&gt;
         return cmp(that) &amp;lt; 0;&lt;br /&gt;
     }&lt;br /&gt;
     bool operator == (const BigSignedInteger &amp;amp;that) const {&lt;br /&gt;
         return cmp(that) == 0;&lt;br /&gt;
     }&lt;br /&gt;
     BigSignedInteger operator + (const BigSignedInteger &amp;amp;that) const {&lt;br /&gt;
         if (sgn == 1) {&lt;br /&gt;
             if (that.sgn == 1)&lt;br /&gt;
                 return BigSignedInteger(a + that.a, 1);&lt;br /&gt;
             if (that.sgn == 0)&lt;br /&gt;
                 return *this;&lt;br /&gt;
             if (that.sgn == -1) {&lt;br /&gt;
                 if (a &amp;gt; that.a)&lt;br /&gt;
                     return BigSignedInteger(a - that.a, 1);&lt;br /&gt;
                 if (a == that.a)&lt;br /&gt;
                     return 0;&lt;br /&gt;
                 if (a &amp;lt; that.a)&lt;br /&gt;
                     return BigSignedInteger(that.a - a, -1);&lt;br /&gt;
             }&lt;br /&gt;
         }&lt;br /&gt;
         if (sgn == 0) {&lt;br /&gt;
             return that;&lt;br /&gt;
         }&lt;br /&gt;
         if (sgn == -1) {&lt;br /&gt;
             if (that.sgn == 1) {&lt;br /&gt;
                 if (a &amp;gt; that.a)&lt;br /&gt;
                     return BigSignedInteger(a - that.a, -1);&lt;br /&gt;
                 if (a == that.a)&lt;br /&gt;
                     return 0;&lt;br /&gt;
                 if (a &amp;lt; that.a)&lt;br /&gt;
                     return BigSignedInteger(that.a - a, 1);&lt;br /&gt;
             }&lt;br /&gt;
             if (that.sgn == 0)&lt;br /&gt;
                 return *this;&lt;br /&gt;
             if (that.sgn == -1)&lt;br /&gt;
                 return BigSignedInteger(a + that.a, -1);&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     BigSignedInteger operator - (const BigSignedInteger &amp;amp;that) const {&lt;br /&gt;
         if (sgn == 1) {&lt;br /&gt;
             if (that.sgn == 1) {&lt;br /&gt;
                 if (a &amp;gt; that.a)&lt;br /&gt;
                     return BigSignedInteger(a - that.a, 1);&lt;br /&gt;
                 if (a == that.a)&lt;br /&gt;
                     return 0;&lt;br /&gt;
                 if (a &amp;lt; that.a)&lt;br /&gt;
                     return BigSignedInteger(that.a - a, -1);&lt;br /&gt;
             }&lt;br /&gt;
             if (that.sgn == 0)&lt;br /&gt;
                 return *this;&lt;br /&gt;
             if (that.sgn == -1)&lt;br /&gt;
                 return BigSignedInteger(a + that.a, 1);&lt;br /&gt;
         }&lt;br /&gt;
         if (sgn == 0) {&lt;br /&gt;
             return BigSignedInteger(that.abs(), -that.sign());&lt;br /&gt;
         }&lt;br /&gt;
         if (sgn == -1) {&lt;br /&gt;
             if (that.sgn == 1)&lt;br /&gt;
                 return BigSignedInteger(a + that.a, -1);&lt;br /&gt;
             if (that.sgn == 0)&lt;br /&gt;
                 return *this;&lt;br /&gt;
             if (that.sgn == -1) {&lt;br /&gt;
                 if (a &amp;gt; that.a)&lt;br /&gt;
                     return BigSignedInteger(a - that.a, -1);&lt;br /&gt;
                 if (a == that.a)&lt;br /&gt;
                     return 0;&lt;br /&gt;
                 if (a &amp;lt; that.a)&lt;br /&gt;
                     return BigSignedInteger(that.a - a, 1);&lt;br /&gt;
             }&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     BigSignedInteger operator * (const BigSignedInteger &amp;amp;that) const {&lt;br /&gt;
         return BigSignedInteger(a * that.a, sgn * that.sgn);&lt;br /&gt;
     }&lt;br /&gt;
     BigSignedInteger operator / (const BigSignedInteger &amp;amp;that) const {&lt;br /&gt;
         if (a &amp;lt; that.a)&lt;br /&gt;
             return 0;&lt;br /&gt;
         return BigSignedInteger(a / that.a, sgn / that.sgn);&lt;br /&gt;
     }&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
== Длинные рациональные числа со знаком ==&lt;br /&gt;
&lt;br /&gt;
 class BigRational {&lt;br /&gt;
     BigSignedInteger n;&lt;br /&gt;
     BigInteger d;&lt;br /&gt;
     void normalize() {&lt;br /&gt;
         BigInteger g = n.abs().gcd(d);&lt;br /&gt;
         n = n / g;&lt;br /&gt;
         d = d / g;&lt;br /&gt;
     }&lt;br /&gt;
 public:&lt;br /&gt;
     BigRational(long long n = 0, long long d = 1) : n(n), d(d) {&lt;br /&gt;
         normalize();&lt;br /&gt;
     }&lt;br /&gt;
     BigRational(const BigSignedInteger &amp;amp;n, const BigInteger &amp;amp;d) : n(n), d(d) {&lt;br /&gt;
         normalize();&lt;br /&gt;
     }&lt;br /&gt;
     BigSignedInteger numerator() const {&lt;br /&gt;
         return n;&lt;br /&gt;
     }&lt;br /&gt;
     int cmp(const BigRational &amp;amp;that) const {&lt;br /&gt;
         return (n * that.d).cmp(that.n * d);&lt;br /&gt;
     }&lt;br /&gt;
     bool operator &amp;lt; (const BigRational &amp;amp;that) const {&lt;br /&gt;
         return cmp(that) &amp;lt; 0;&lt;br /&gt;
     }&lt;br /&gt;
     bool operator == (const BigRational &amp;amp;that) const {&lt;br /&gt;
         return cmp(that) == 0;&lt;br /&gt;
     }&lt;br /&gt;
     BigRational operator + (const BigRational &amp;amp;that) const {&lt;br /&gt;
         return BigRational(n * that.d + that.n * d, d * that.d);&lt;br /&gt;
     }&lt;br /&gt;
     BigRational operator - (const BigRational &amp;amp;that) const {&lt;br /&gt;
         return BigRational(n * that.d - that.n * d, d * that.d);&lt;br /&gt;
     }&lt;br /&gt;
     BigRational operator * (const BigRational &amp;amp;that) const {&lt;br /&gt;
         return BigRational(n * that.n, d * that.d);&lt;br /&gt;
     }&lt;br /&gt;
     BigRational operator / (const BigRational &amp;amp;that) const {&lt;br /&gt;
         if (that.n.sign() == 1)&lt;br /&gt;
             return BigRational(n * that.d, that.n.abs() * d);&lt;br /&gt;
         if (that.n.sign() == -1)&lt;br /&gt;
             return BigRational(n * that.d * -1, that.n.abs() * d);&lt;br /&gt;
     }&lt;br /&gt;
 };&lt;/div&gt;</summary>
		<author><name>Ctrlalt</name></author>
	</entry>
</feed>