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