UVa 11371-number Theory for Newbies

Source: Internet
Author: User

Title: give you a number n. The number of each bit in the area is then combined to form a A, a, B. Makes the A-B maximum and 9 times.

Analysis: Mathematical theory.

The problem requires that the number of a, B and N is the same 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. Each of these letters represents a bit. The corresponding value can be the same,

Then arbitrarily exchange two bits I. The new number obtained by J is ABC. J.. I.. XWZ, the difference is 9. 90..0 * (I-J).

So it must be a multiple of 9, proof.

Through the above theorem can continue to prove. The difference between the new number and the original number of random bit numbers is 9 times the number of characters.

So take a for maximum combination. That is, by descending order, B can be added by order.

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

Then, exchange the first non-0 number and the first 1 can be.

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;}


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.