UVa 11371-number Theory for Newbies

Source: Internet
Author: User

Title: give you a number n, the number of each bit in the back of the formation of a A, a, so that a-B maximum and 9 times.

Analysis: Mathematical theory. The problem requires the same number of digits as a, B and N and cannot have a predecessor 0.

Theorem 1: The number of a number of two bits in a number, the difference between the new number and the original number is 9 times the sum of the numbers;

Evidence 1: The digital word is ABC. I.. J...XWZ, where each letter represents a bit, the corresponding value can be the same,

Then randomly swap two-bit i,j to get a new number for ABC. J.. I.. XWZ, the difference is 9. 90..0 * (I-J),

So it must be a multiple of 9, proof.

The above theorem can continue to prove that the difference between the new number and the original number of any digit is 9 times.

Therefore, a is the largest combination, that is, the descending order, B for the order can be added;

However, this requires the same number of bits, so B is not the minimum value of 0 digits, first to take the order of the increment,

Then, swap the first non-0 and first 1.

stating: ╮(╯▽╰)╭.

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace Std;bool cmp1 (char A, char b) {return a < b;} BOOL Cmp2 (char A, char b) {return a > B;} int main () {char buf[31];while (gets (BUF)) {int len = strlen (BUF); long long A = 0LL, B = 0ll;sort (buf, Buf+len, CMP1); if (b Uf[0] = = ' 0 ') {for (int i = 0; i < len; + + i) if (buf[i]! = ' 0 ') {swap (Buf[i], buf[0]); for (int i = 0; i < len; + + i) A = a*10ll + buf[i]-' 0 "; sort (buf, Buf+len, CMP2); for (int i = 0; i < len; + + i) B = b* 10LL + buf[i]-' 0 '; printf ("%lld-%lld =%LLD = 9 *%lld\n", b,a,b-a, (b-a)/9ll);}    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVa 11371-number Theory for Newbies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.