Poj1117 pairs of Integers (simple DFS)

Source: Internet
Author: User
Tags integer numbers
Pairs of Integers
Time limit:1000 ms   Memory limit:10000 K
Total submissions:3622   Accepted:905

Description

You are to find all pairs of integers such that their sum is equal to the given integer number N and the second number results from the first one by striking out one of its digits. the first integer always has at least two digits and starts with a non-zero
Digit. The second integer always has one digit less than the first integer and may start with a zero digit.

Input

The input file consists of a single integer N (10 <= n <= 10 ^ 9 ).

Output

On the first line of the output file write the total number of different pairs of integers that satisfy the problem statement. on the following lines write all those pairs. write one pair on a line in ascending order of the first integer in the pair. each pair
Must be written in the following format:

X + y = N

Here X, Y, and N, must be replaced with the corresponding integer numbers. There shoshould be exactly one space on both sides of '+' and '= 'characters.

Sample Input

 
302

Sample output

 
5251 + 51 = 302275 + 27 = 302276 + 26 = 302281 + 21 = 302301 + 01 = 302

Source

Northeastern Europe 2001.

To give a number N, find all the formulas that satisfy a + B = N and satisfy the constraint: A can be obtained by adding a number through B, and B can have a front 0, A is not allowed.

Question Analysis: The data range N is 10 ^ 9, so theoretically there are 10 ^ 9 states, but due to constraints, there are far fewer actual states.

First, consider parity. If n is an odd number, it is only possible to add a single digit to B to satisfy the formula. Therefore, 10 * B + B + Add = n, here, add is the last bit of A, and the high position of a is the same as that of B, so we can see that N % 11 = add, because 0 <= add <= 9, therefore, the odd number of N % 11 = 10 is unsolvable. Other solutions are available.

Directly enumerate the position of the number to be added, and then enumerate the number 0-9 at the added position. Because of the existence of the divisor condition, many unreasonable states are lost.

However, the output of this question is DT, so proceed with caution.

For more information, seeCode:

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; typedef _ int64 ll; ll N; int Len, num; ll ans [1000]; ll power [15] = {10,100,100, 10000000,100, 000000, 000000ll, 000000000ll, 0000000000ll}; void DFS (INT POs, int DP, ll S1, ll S2) {// printf ("% d: % d \ n", POs, DP, S1, S2); // system ("pause "); if (dp = Len + 1) {// int dig = S2/power [DP-1]; // S1 = dig * Power [DP] + S1; If (S1 + S2 = N & S2/power [DP-1] = 0) ans [num ++] = S2; return;} If (dp <POS) {for (INT I = 0; I <= 9; I ++) {int T1 = I * power [DP] + S1; if (t1 + T1) % power [DP + 1] = n % power [DP + 1]) DFS (Pos, DP + 1, T1, T1 );}} if (dp = POS) {for (INT I = 0; I <= 9; I ++) {for (Int J = 0; j <= 9; j ++) {int T1 = I * power [DP] + S1; int t2 = J * power [DP] + S2; If (t1 + T2) % power [DP + 1] = = N % power [DP + 1]) {DFS (Pos, DP + 1, T1, T2); Continue ;}}} if (dp> POS) {int dig = S2/power [DP-1]; S1 = dig * power [DP] + S1; For (INT I = 0; I <= 9; I ++) {int t2 = I * power [DP] + S2; If (S1 + T2) % power [DP + 1] = n % power [DP + 1]) DFS (Pos, DP + 1, S1, T2) ;}} int main () {While (scanf ("% i64d", & N )! = EOF) {int M = N; Len = num = 0; while (m) {Len ++; M/= 10;} int I; // for (I = 0; I <= 9; I ++) // If (n + I) % 11 = 0 & N % 11 <10) // ans [num ++] = N/11; for (I = 1; I <= Len; I ++) DFS (I, 1, 0, 0 ); sort (ANS, ANS + num); int TTT = 0; for (I = num-1; I> = 0; I --) {if (I <num-1 & Ans [I] = ans [I + 1]) TTT ++; if (ANS [I] + ans [I] = N) TTT ++;} printf ("% d \ n", num-TTT ); for (I = num-1; I> = 0; I --) {If (I <num-1 & Ans [I] = ans [I + 1]) continue; If (ANS [I] + ans [I] = N) continue; int TMP = ans [I]; int CNT = 1; while (TMP/10) {CNT ++; tmp/= 10;} int cnt2 = 1; TMP = N-ans [I]; while (TMP/10) {cnt2 ++; tmp/= 10;} printf ("% i64d + ", n-ans [I]); For (Int J = 1; j <cnt2-CNT; j ++) // note that 0 is added before the second number... Printf ("0"); printf ("% i64d", ANS [I]); printf ("= % i64d \ n", n) ;}} return 0 ;} // 176k47ms

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.