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